How to force an IntentService to stop immediately with a cancel button from an Activity?

后端 未结 9 882
梦如初夏
梦如初夏 2020-11-30 00:10

I have an IntentService that is started from an Activity and I would like to be able to stop the service immediately from the activity with a \"cancel\" button in the activi

相关标签:
9条回答
  • 2020-11-30 01:10

    I've used a BroadcastReceiver inside the service that simply puts a stop boolean to true. Example:

    private boolean stop=false;
    
    public class StopReceiver extends BroadcastReceiver {
    
       public static final String ACTION_STOP = "stop";
    
       @Override
       public void onReceive(Context context, Intent intent) {
           stop = true;
       }
    }
    
    
    @Override
    protected void onHandleIntent(Intent intent) {
        IntentFilter filter = new IntentFilter(StopReceiver.ACTION_STOP);
        filter.addCategory(Intent.CATEGORY_DEFAULT);
        StopReceiver receiver = new StopReceiver();
        registerReceiver(receiver, filter);
    
        // Do stuff ....
    
        //In the work you are doing
        if(stop==true){
            unregisterReceiver(receiver);
            stopSelf();
        }
    }
    

    Then, from the activity call:

    //STOP SERVICE
    Intent sIntent = new Intent();
    sIntent.setAction(StopReceiver.ACTION_STOP);
    sendBroadcast(sIntent);
    

    To stop the service.

    PD: I use a boolean because In my case I stop the service while in a loop but you can probably call unregisterReceiver and stopSelf in onReceive.

    PD2: Don't forget to call unregisterReceiver if the service finishes it's work normally or you'll get a leaked IntentReceiver error.

    0 讨论(0)
  • 2020-11-30 01:10

    Here is some sample code to start/stop Service

    To start,

    Intent GPSService = new Intent(context, TrackGPS.class);
    context.startService(GPSService);
    

    To stop,

    context.stopService(GPSService);

    0 讨论(0)
  • 2020-11-30 01:11

    Here is the trick, make use of a volatile static variable and check continue condition in some of lines in your service that service continue should be checked:

    class MyService extends IntentService {
        public static volatile boolean shouldContinue = true;
        public MyService() {
            super("My Service");
        }
    
        @Override
        protected void onHandleIntent(Intent intent) {
            doStuff();
        }
    
        private void doStuff() {
            // do something 
    
            // check the condition
            if (shouldContinue == false) {
                stopSelf();
                return;
            }
    
           // continue doing something
    
           // check the condition
           if (shouldContinue == false) {
               stopSelf();
               return;
           }
    
           // put those checks wherever you need
       }
    }
    

    and in your activity do this to stop your service,

     MyService.shouldContinue = false;
    
    0 讨论(0)
提交回复
热议问题