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

前端 未结 11 846
旧巷少年郎
旧巷少年郎 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:26

    It's very common this situation where I need to stop my service before to finish the process. In some case is not enough with stopService(intent). You should have in mind the onDestroy() implement in my service. Example:

    public class MyIntentService extends IntentService {
    
        // Defines and instantiates an object for handling status updates.
        private BroadcastNotifier mBroadcaster = null;
        private int progress = 0; //THIS IS MY COUNTER FOR EXAMPLE!!!
    
        public MyIntentService() {
            super("MyIntentService");
        }
    
        @Override
        protected void onHandleIntent(Intent intent) {
    
            progress = 0;
            int tiempo_disponible = intent.getIntExtra("minutos_disponible", 0);
    
            if (mBroadcaster == null){
    
                mBroadcaster = new BroadcastNotifier(this);
            }
            // Broadcasts an Intent indicating that processing has started.
            mBroadcaster.broadcastIntentWithState(Constants.STATE_ACTION_STARTED);
    
            mBroadcaster.broadcastIntentWithState(Constants.STATE_ACTION_RUNNING);
    
            while (progress < tiempo_disponible) {
    
                progress++;
                try {
                    Log.i(Constants.TAG, "Procesing " + progress);
                    mBroadcaster.notifyProgress(progress);
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            // Reports that the feed retrieval is complete.
            mBroadcaster.broadcastIntentWithState(Constants.STATE_ACTION_COMPLETE);
        }
    
        @Override
        public void onDestroy() {
            progress = 1000000; // WHITH THAT YOU FINISH THE CICLE IF tiempo_disponible NEVER IS MAYOR THAT 1000000, YOU CAN USE OTHER CONDITIONAL!!!!!!
            super.onDestroy();
        }
    } 
    

    In this way, when you have stopped the service using stopService method also you will have stopped the process o counter.

    public void stopService(){
            context.stopService(intent);
            LocalBroadcastManager.getInstance(context).unregisterReceiver(responseReceiver);
            responseReceiver = null;
            intent = null;
    }
    

    Take care! @yaircarreno

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

    If you are tracking GPS location, you probably used GoogleApiClient.

    The concept is that the Service WILL NOT stop,

    if an GoogleApiClient instance is still connected within it.

    (Or any other issue that need to be destroyed / unregistered first)

    So to make it works, implement onDestroy() within your service:

    @Override
    public void onDestroy()
    {
        // Unregistered or disconnect what you need to
        // For example: mGoogleApiClient.disconnect();
        super.onDestroy();
    }
    
    0 讨论(0)
  • 2020-12-03 07:27

    @Override

    public void onDestroy() {
    
        Log.d(TAG, "onDestroy");
        super.onDestroy();
        if (mLocationManager != null) {
    
            for (int i = 0; i < mLocationListeners.length; i++) {
    
                try {
    
                    mLocationManager.removeUpdates(mLocationListeners[i]);
    
                } catch (Exception ex) {
    
                    Log.d(TAG, "fail to remove location listners, ignore", ex);
    
                }
    
            }
    
        }
    
    }
    
    0 讨论(0)
  • 2020-12-03 07:32

    In my case the stopService is called with startService almost simultaneously so no service is there to be stopped. Try delay stopService for a few seconds. :)

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

    it could be perhaps that you are creating a new Intent everytime you call the stop service.

    stopService(new Intent(GPSLoc.this, MyService.class));
    

    perhaps try :

    Intent intnet = new Intent(GPSLoc.this, MyService.class); // create el service
    
    startService(intenet); 
    stopService(intent);
    
    0 讨论(0)
  • 2020-12-03 07:41

    I have found the best way to stop a service is to make stop itself. This way you are sure it actually will stop and preserve data integrity. If you want to do it from outside (activity) I usually use a global static attribute.

    Per example (Kotlin) if I have MyService, MyActivity and MyObject

    My Object

    object MyObject{
        abort = false
    }
    

    MyService

    override fun onHandleIntent(intent: Intent?) {
        startForeground(id,notification)   
        for (i in range){
            if (MyObject.abort) break
            // RUN SOME CODE HERE
        }
        stopForeground(true)
        stopSelf()
    }
    

    MyActivity

    fun startService() {
        startForegroundService(Intent(this, OptimizationService::class.java))
    }
    fun stopService() {
        MyObject.abort = true
    }
    
    0 讨论(0)
提交回复
热议问题