Pass Service From one Activity to Another

2019-07-23 16:45发布

how do I pass a service from one activity to another? I have a music player (Activity1) that displays a list of songs, and when you click on it, it starts the service within Activity1. I have a button that users can click that will open Activity2.

So what is the best way for me to pass the service from Activity1 to Activity2. If the service is started in Activity1, then Activity2 should continue playing. If the service is not started in Activity1, then Activity2 should start the service before using it.

Thanks.

Here's some sample code, the MusicService is a class that extends the service class.

 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) {

    }
};

}

2条回答
对你真心纯属浪费
2楼-- · 2019-07-23 16:59

you can define Application extend class and use public static variable in this class and access this variable in all activity .

like this :

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

and in manifest :

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

Now you can access G.serviceMusic any where.

查看更多
劫难
3楼-- · 2019-07-23 17:09

I think it would be better to bind service instead of pass a static global service. Since you have multiple activities which will use the same service, a base activity should be created like this:

BasicServiceActivity.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.
    }
}

Then you can implement subclass activity like this:

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);
    }
}
查看更多
登录 后发表回答