我正在开发一款用于在线播放歌曲的应用。我可以点击它播放这首歌。
我的问题是我在所有活动中都有一个CircularSeekBar,我需要在所有屏幕上管理它。如果从任何活动播放歌曲,则所有屏幕上都应显示CircularSeekBar。我能做的是当播放一首歌时,我能够在该特定活动上显示搜索栏,但无法在任何其他活动中管理该搜索栏。
以下是我的一个xml文件的示例:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical">
</LinearLayout>
<include layout="@layout/seek_bar_layout"></include>
</android.support.design.widget.CoordinatorLayout>
请告诉我如何在所有活动中管理该搜索栏。
先谢谢。
答案 0 :(得分:2)
当其他可见时,您无法管理活动视图。
您可以通过任何boolean
标记解析它(例如:isSongPlaying = false
)。当您开始播放歌曲时,请将其设置为true
。在onResume中的每个活动中,检查该标记并显示/隐藏您的SeekBar
。
尽量避免使用public static
并使用任何其他方式。
根据您的评论,我创建了一个非常简单的示例,说明如何使用服务解决问题。请记住,此代码需要进行大量改进。
<强> SongService 强>
public class SongService extends Service {
ActivityManager activityManager;
IBinder iBinder;
boolean playingSong = false;
Worker worker;
public SongService() {
iBinder = new MyBinder();
}
private void playSong(String path) {
if(worker != null) {
worker.cancel(true);
}
worker = new Worker();
worker.execute();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return iBinder;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
private class Worker extends AsyncTask<Void, Integer, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
playingSong = true;
}
@Override
protected Void doInBackground(Void... params) {
for (int i = 100; i > -1; i--) {
publishProgress(i);
try {
Thread.sleep(100l);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
if (activityManager != null) {
activityManager.publishProgress(values[0]);
}
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
playingSong = false;
activityManager.onSongFinish();
}
}
public class MyBinder extends Binder {
public SongService getSongService() {
return SongService.this;
}
public void BindView(ActivityManager activityManager) {
SongService.this.activityManager = activityManager;
}
public void playSong(String path) {
SongService.this.playSong(path);
}
public boolean isPlayingSong() {
return playingSong;
}
}
public interface ActivityManager {
void publishProgress(int progress);
void onSongFinish();
}
}
<强> MainActivity:强>
public class MainActivity
extends AppCompatActivity
implements ServiceConnection, SongService.ActivityManager {
TextView progress;
Button play;
Button next;
SongService.MyBinder myBinder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progress = (TextView) findViewById(R.id.progress);
play = (Button) findViewById(R.id.play);
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(myBinder != null) {
//Your path to song
myBinder.playSong("");
play.setEnabled(false);
}
}
});
next = (Button) findViewById(R.id.next);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
Intent mIntent = new Intent(this, SongService.class);
bindService(mIntent, this, BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
super.onStop();
unbindService(this);
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
if(service instanceof SongService.MyBinder) {
myBinder = (SongService.MyBinder) service;
myBinder.BindView(this);
if (myBinder.isPlayingSong()) {
play.setEnabled(false);
}
else {
play.setEnabled(true);
}
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
myBinder = null;
}
@Override
public void publishProgress(int progress) {
this.progress.setText("Progress: " + progress);
}
@Override
public void onSongFinish() {
play.setEnabled(true);
}
}
<强> MainActivity2 强>
此活动类与MainActivity看起来非常相似,但具有不同的布局文件。
MainActivity - 布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.aszymanski2.serviceanddelegatepatternexample.MainActivity">
<TextView
android:id="@+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="Play"
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"/>
<Button
android:text="SecondActivity"
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>
MainActivity2 - 布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.aszymanski2.serviceanddelegatepatternexample.MainActivity">
<TextView
android:id="@+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="Play"
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"/>
<Button
android:text="Back"
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>