Good practices for services on Android

前端 未结 3 1216
清酒与你
清酒与你 2021-01-23 05:05

I am currently using 2 services in my app:

1: LocationService, basically trying to localize the user, and aims to stay alive only when the app is on for

相关标签:
3条回答
  • 2021-01-23 05:32

    It hasn't been well-documented in Google's official dev guide, Context.bindService() is actually an asynchronous call. This is the reason why ServiceConnection.onServiceConnected() is used as a callback method, means not happened immediately.

    public class MyActivity extends Activity {
      private MyServiceBinder myServiceBinder;
    
      protected ServiceConnection myServiceConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
          myServiceBinder = (MyServiceBinderImpl) service;
        }
    
        ... ...
      }
    
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // bindService() is an asynchronous call. myServiceBinder is resoloved in onServiceConnected()
        bindService(new Intent(this, MyService.class),myServiceConnection, Context.BIND_AUTO_CREATE);
        // You will get a null point reference here, if you try to use MyServiceBinder immediately.
        MyServiceBinder.doSomething(); // <-- not yet resolved so Null point reference here
      }
    }
    

    A workaround is call MyServiceBinder.doSomething() in myServiceConnection.onServiceConnected(), or perform MyServiceBinder.doSomething() by some user interaction (e.g. button click), as the lag after you call bindService() and before system get a reference of myServiceBinder is quite soon. as long as you are not using it immediately, you should be just fine.

    Check out this SO question CommonsWare's answer for more details.

    0 讨论(0)
  • 2021-01-23 05:41

    this thread is quite old, but I just discovered it.

    Actually there is only one way for your service to go on living if it is bound : it has to be also started. The documentation is not quite clear about that but a service can be both started and bound.

    In that case, the service will not get destroyed when you unbind from it, it will get destroyed when :

    • you stop it and there is no one bound to it
    • you unbind from it and it has been stopped before.

    I made a small Service Lifecycle demo app on GitHub and it's also available on Google Play.

    Hope that helps ;)

    0 讨论(0)
  • 2021-01-23 05:46

    if you bind to a service in an Activity, you need to unbind it too:

    @Override
    protected void onResume() {
    
        Log.d("activity", "onResume");
        if (locationServiceBinder == null) {
            doBindLocationService();
        }
                super.onResume();
    }
    
    @Override
    protected void onPause() {
    
        Log.d("activity", "onPause");
        if (locationServiceBinder  != null) {
            unbindService(callConnectService);
            locationServiceBinder = null;
        }
        super.onPause();
    }
    

    where doBindLocationService():

    public void doBindLocationService() {
        Log.d("doBindService","called");
    
        aimConServ =  new Intent(this, LocationService.class);
        // Create a new Messenger for the communication back
        // From the Service to the Activity
        bindService(aimConServ, callConnectService, Context.BIND_AUTO_CREATE);
    }
    

    You need to do this practise for your XmppService as well

    0 讨论(0)
提交回复
热议问题