How do I cancel all pending intents that are qued for intent Service

守給你的承諾、 提交于 2019-12-04 09:55:09
Aldryd

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.

18446744073709551615

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!