如何取消意向服务排队的所有待定意向 [英] How do I cancel all pending intents that are qued for intent Service

查看:137
本文介绍了如何取消意向服务排队的所有待定意向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个intentservice,它由用户和我的应用自动排队.当用户注销我的应用程序时,我需要能够杀死所有待解决的意图,但是我似乎无法使其正常工作.我已经尝试过stopService()和stopself(),但是在用户注销后,这些意图会继续触发intentservice.我会尝试获取意图的ID,但这很困难,因为每次intentservice启动时,保存意图ID的变量都是空的.这是我的intentservice代码:

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
}

推荐答案

不幸的是,我认为无法使用标准的IntentService方法来完成该任务,因为它没有提供一种在已经运行时就可以中断它的方法.

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. 复制IntentService代码以对其进行自己的修改,从而使您可以删除待处理的消息.看起来有人在这里取得了一些成功:在使用IntentService时,我有类似的感觉,最终我只是将其转换为Service.这样,我可以同时运行任务,并且在需要清除它们时也可以取消它们.

    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.

    这篇关于如何取消意向服务排队的所有待定意向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆