将服务从一个活动传递到另一个活动



如何将服务从一个活动传递到另一个活动?我有一个音乐播放器(Activity1(,它显示歌曲列表,当你点击它时,它会启动Activity1中的服务。我有一个按钮,用户可以点击它来打开Activity2。

那么,对我来说,将服务从Activity1传递到Activity2的最佳方式是什么呢。如果服务是在Activity1中启动的,那么Activity2应该继续播放。如果服务没有在Activity1中启动,那么Activity2应该在使用之前启动服务。

谢谢。

下面是一些示例代码,MusicService是一个扩展服务类的类。

 public class Activity1 extends AppCompatActivity {
 private MusicService serviceMusic;  
private ServiceConnection musicConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        MusicService.PlayerBinder binder = (MusicService.PlayerBinder) service;
        //get service
        serviceMusic = binder.getService();
        serviceMusic.setSongList(songList);
    }
    @Override
    public void onServiceDisconnected(ComponentName name) {
    }
};

}

我认为最好绑定服务,而不是传递静态全局。由于您有多个活动将使用相同的服务,因此应该创建一个基本活动,如下所示:

基本服务活动.java

public abstract class BasicServiceActivity extends AppCompatActivity {
    protected DvrService mDvrService;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_basic);
        attachService();
    }
    @Override
    protected void onDestroy() {
        detachService();
        super.onDestroy();
    }
    private ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder binder) {
            DvrService.DvrServiceBinder serviceBinder = (DvrService.DvrServiceBinder) binder;
            mDvrService = serviceBinder.getService();
            onServiceAttached(mDvrService);
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {
            mDvrService = null;
        }
    };
    private void attachService() {
        Intent service = new Intent(this, DvrService.class);
        bindService(service, mServiceConnection, Service.BIND_AUTO_CREATE);
    }
    private void detachService() {
        unbindService(mServiceConnection);
    }
    /** Callback when service attached. */
    protected void onServiceAttached(DvrService service) {
        // do something necessary by its subclass.
    }
}

然后您可以实现这样的子类活动:

public class ServiceActivity extends BasicServiceActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        startService(new Intent(this, DvrService.class));
    }
    @Override
    protected void onDestroy() {
        if (mDvrService != null) {
            mDvrService.removeListener1(mListener1);
        }
        super.onDestroy();
    }
    @Override
    protected void onServiceAttached(DvrService service) {
        // do your stuff, for example add a listener.
        service.addListener1(mListener1);
    }
}

您可以定义应用程序扩展类,并在该类中使用公共静态变量,并在所有活动中访问该变量。

像这样:

public class G extends Aplication{
   public static MusicService serviceMusic;  
}

并且在清单中:

  <application ...
    android:name=".G">

现在,您可以在任何位置访问G.serviceMusic。

相关内容

  • 没有找到相关文章

最新更新