Pass Service From one Activity to Another

微笑、不失礼 提交于 2019-12-12 22:21:22

问题


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

    }
};

}


回答1:


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.




回答2:


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);
    }
}


来源:https://stackoverflow.com/questions/38780522/pass-service-from-one-activity-to-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!