stopService doesn't stop's my service… why?

前端 未结 11 909
旧巷少年郎
旧巷少年郎 2020-12-03 07:02

i have a background service on my android APP that is getting my GPS position and sending it to a remote db. It work\'s fine.

The problem is when i want to stop the

11条回答
  •  猫巷女王i
    2020-12-03 07:44

    I had the same problem. I found that if the service has GoogleApiClient connected and still get location update, the stopService() has totally no effect, the service's industry() was not called. To fix the problem, I created a function to stop the location service in the service code. Call the stopLocationService() from the activity, and then call stopService. Here is the code example:

    public class myLocationService extends Service{
    ...
    
        public void stopLocationUpdates() {
    
            LocationService.FusedLocationApi.removeLocationUpdates(mGoogleApiClient,this);       
            mGoogleApiClient.disconnect();
    
        }
        ...
    } 
    

    In activity,

    {
        ...
        if(mService != null && isBound) {
    
            mService.stopLocationUpdates();
            doUnbindService();
            stopService(new Intent(this,   myLocationService.class));
    
         }
         ...
    } 
    

提交回复
热议问题