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

前端 未结 11 847
旧巷少年郎
旧巷少年郎 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条回答
  • 2020-12-03 07:42

    i am sure that it's not stopped cause on my database i still recive gps positions from the emulator when i have press the button to stop the service.

    You probably are not unregistering your LocationListener.

    0 讨论(0)
  • 2020-12-03 07:43

    For those who want to send a request to server periodically, this is my solution. You should have this in your Activity or Fragment Activity

    {
    private static final Long UPDATE_LOCATION_TIME  = 30 * 60 * 1000l; // 30 minute
    
    private AlarmManager alarm;
    private PendingIntent pIntent; 
    ...
    
    @Override
        protected void onResume() {
            super.onResume();
    
            // Run background service in order to update users location
            startUserLocationService();
    
            Log.e(TAG, "onResume");
        }
    
        @Override
        protected void onStop() {
            super.onStop();
    
            stopUserLocationService();
    
            Log.e(TAG, "onStop");
        }
    
    private void startUserLocationService() {
            Log.i(TAG, "Starting service...");
            Intent intent = new Intent(MainFragmentHolder.this, ServiceUserLocation.class);
            pIntent = PendingIntent.getService(this, 0, intent, 0);
    
            alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
            Calendar cal = Calendar.getInstance();
            alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), UPDATE_LOCATION_TIME, pIntent);
        }
    
        private void stopUserLocationService() {
            alarm.cancel(pIntent);
            Intent intent = new Intent(MainFragmentHolder.this, ServiceUserLocation.class);
            stopService(intent);
        }
    
    }
    
    0 讨论(0)
  • 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));
    
         }
         ...
    } 
    
    0 讨论(0)
  • 2020-12-03 07:45

    my problem solved by removing the added views to WindowManager ondestroy

    public void onDestroy() {
        isRunning = false;
        super.onDestroy();
        if (checkBox!=null) {
            windowManager.removeView(getlayoutparm(fabsetting,fabrateus,fabexit,true)); 
            windowManager.removeView(checkBox); 
        }
     }
    
    0 讨论(0)
  • 2020-12-03 07:47

    Have you implemented onDestroy()? If not, I believe that might be the solution - and you stop your Timer or whatever you're using to run the service within onDestroy().

    A service can be stopped by calling its stopSelf() method, or by calling Context.stopService().

    See this link for some more information.

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