Good practices for services on Android

前端 未结 3 1218
清酒与你
清酒与你 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: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

提交回复
热议问题