How to cancel all pending intentions that are sent to the intent?

I have an intenservice that is automatically started by the user and my application. I need to be able to kill all pending intentions that are executed when the user exits my application, but I cannot get it to work. I tried stopService () and stopself (), but intentions continue to disable intentservice after the user logs out. I would try to get the intent identifier, but this is difficult, since every time an intentservice is started, the variable containing the id of the target is empty. Here is my intenservice 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 } 
+4
source share
3 answers

Unfortunately, I do not think that this can be done with the standard IntentService methods, since it does not offer a way to interrupt it while it is already running.

There are several options that I can think of, that you can try to find out if they fit your needs.

  • Copy the IntentService code to make your own changes to it, which will allow you to delete pending messages. Someone seems to have had some success: Android: intenservice, how to abort or skip a task in a queue with an ad.
  • Instead of copying all the IntentService code, you can also bind it to a regular service (since IntentService extends the service), so you can write your own function to delete the waiting messages. It is also mentioned in this link.
  • Rewrite IntentService as a regular service. With this option, you will have more control over adding and deleting messages.

I had what looked like a similar situation when I used IntentService, and in the end I just converted it to a service. This allows me to run tasks at the same time, as well as cancel them when I need to clean them.

+4
source

Here When should I release my own (Android NDK) descriptors? is a HangAroundIntentService class that has a cancelQueue() method. The class also has a method.

public static Intent markedAsCancelIntent(Intent intent)

which converts the intention to the intention of cancellation, and

public static boolean isCancelIntent(Intent intent) .

The class is based on open source Google.

0
source

Just a thought, but inside your onhandleintent you have an argument that checks if the application is working, if not, then don't run the code? example. At the beginning of your application, you can have a static var

 boolean appRunning; 

Further, in your intention, when you set appRunning to false, after the onPause or onDestroy action, you can wrap the onhandleintentent code in a logical state:

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

Just a thought

0
source

Source: https://habr.com/ru/post/1384347/


All Articles