How to force an IntentService to stop immediately

2019-01-10 18:57发布

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 activity. As soon as that "cancel" button is pressed, I want the service to stop executing lines of code.

I've found a number of questions similar to this (i.e. here, here, here, here), but no good answers. Activity.stopService() and Service.stopSelf() execute the Service.onDestroy() method immediately but then let the code in onHandleIntent() finish all the way through before destroying the service.

Since there is apparently no guaranteed way to terminate the service's thread immediately, the only recommended solution I can find (here) is to have a boolean member variable in the service that can be switched in the onDestroy() method, and then have just about every line of the code in onHandleIntent() wrapped in its own "if" clause looking at that variable. That's an awful way to write code.

Does anybody know of a better way to do this in an IntentService?

9条回答
男人必须洒脱
2楼-- · 2019-01-10 19:06

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;
查看更多
虎瘦雄心在
3楼-- · 2019-01-10 19:07

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);

查看更多
三岁会撩人
4楼-- · 2019-01-10 19:09
@Override
protected void onHandleIntent(Intent intent) {
    String action = intent.getAction();
    if (action.equals(Action_CANCEL)) {
        stopSelf();
    } else if (action.equals(Action_START)) {
        //handle
    }
}

Hope it works.

查看更多
\"骚年 ilove
5楼-- · 2019-01-10 19:09
context.stopService(GPSService);
查看更多
The star\"
6楼-- · 2019-01-10 19:14

In case of IntentService it does not stop or takes any other request through some intent action until its onHandleIntent method completes the previous request.

If we try to start IntentService again with some other action, onHandleIntent will be called only when previous intent / task is finished.

Also stopService(intent); or stopSelf(); does not work until the onHandleIntent() method finishes its task.

So I think here better solution is to use normal Service here.

I hope it will help!

查看更多
手持菜刀,她持情操
7楼-- · 2019-01-10 19:15

Stopping a thread or a process immediately is often a dirty thing. However, it should be fine if your service is stateless.

Declare the service as a separate process in the manifest:

<service
     android:process=":service"
     ...

And when you want to stop its execution, just kill that process:

ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> runningAppProcesses = am.getRunningAppProcesses();

Iterator<RunningAppProcessInfo> iter = runningAppProcesses.iterator();

while(iter.hasNext()){
    RunningAppProcessInfo next = iter.next();

    String pricessName = getPackageName() + ":service";

    if(next.processName.equals(pricessName)){
        Process.killProcess(next.pid);
        break;
    }
}
查看更多
登录 后发表回答