How do I cancel all pending intents that are qued

2019-04-09 18:50发布

I have an intentservice that gets qued by the user and by my app automatically. I need to be able to kill all pending intents that are qued when the user logs out of my application, but I cannot seem to get that to work. I have tried stopService() and stopself(), but the intents continue to fire off the intentservice after the user has logged out. I would try to get the id of the intent but that is difficult as everytime the intentservice starts, the variable holding the intent id's is empty. Here is my intentservice code:

public class MainUploadIntentService extends IntentService {
private final String TAG = "MAINUPLOADINTSER";
private GMLHandsetApplication app = null;
private SimpleDateFormat sdf = null;
public boolean recStops = true;

public MainUploadIntentService() {
    super("Main Upload Intent Service");

    GMLHandsetApplication.writeToLogs(TAG,
            "GMLMainUploadIntentService Constructor");

}

@Override
protected void onHandleIntent(Intent intent) {
GMLHandsetApplication.writeToLogs(TAG, "onHandleIntent Started");
if (app == null) {
    app = (GMLHandsetApplication) getApplication();
}
uploadData(app);
    GMLHandsetApplication.writeToLogs(TAG, "onHandleIntent Finished");
}

@Override
public void onDestroy() {
GMLHandsetApplication.writeToLogs(TAG, "onDestroy Started");
app = null;
    stopSelf();
    GMLHandsetApplication.writeToLogs(TAG, "onDestroy completed");
}

public void uploadData(GMLHandsetApplication appl) {
    //All of my code that needs to be ran
}

3条回答
做自己的国王
2楼-- · 2019-04-09 19:17

Here When should I free the native (Android NDK) handles? is the HangAroundIntentService class that has the method cancelQueue(). The class also has the method

public static Intent markedAsCancelIntent(Intent intent)

that converts an intent into a cancel intent, and

public static boolean isCancelIntent(Intent intent).

The class is based on the open-sourced Google's code.

查看更多
三岁会撩人
3楼-- · 2019-04-09 19:33

Just a thought but inside of your onhandleintent can you have an argument that checks to see if app is running if not then don't run the code? example. In the start of your app you could have a static var

boolean appRunning;

Next in your onhandle of the intent, when you set the appRunning to false, after an onPause or onDestroy of activity, you could wrap the onhandleintent code in a boolean:

 protected void onHandleIntent(final Intent intent) {
     if(MainActivity.appRunning){
       ...
     }
}

Just a thought

查看更多
放我归山
4楼-- · 2019-04-09 19:34

Unfortunately, I don't think it's possible to accomplish that with the standard IntentService methods since it doesn't offer a way to interrupt it while it's already going.

There are a few options I can think of that you can try to see if they fit your need.

  1. Copy the IntentService code to make your own modifications to it that would allow you to remove pending messages. Looks like someone had some success with that here: Android: intentservice, how abort or skip a task in the handleintent queue
  2. Instead of copying all the IntentService code, you might also be able to Bind to it like a normal Service (since IntentService extends Service) so you can write your own function to remove pending messages. This one is also mentioned in that link.
  3. Rewrite the IntentService as a regular Service instead. With this option, you'd have more control over adding and removing messages.

I had what sounds like a similar situation where I was using an IntentService, and I eventually just converted it to a Service instead. That let me run the tasks concurrently and also cancel them when I needed to clear them.

查看更多
登录 后发表回答