gpt4 book ai didi

android - 如何取消所有排队等待 Intent 服务的未决 Intent

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:14:28 24 4
gpt4 key购买 nike

我有一个由用户和我的应用自动查询的 intentservice。当用户注销我的应用程序时,我需要能够杀死所有被查询的未决 Intent ,但我似乎无法让它工作。我已经尝试过 stopService() 和 stopself(),但是在用户注销后 Intent 继续触发 intentservice。我会尝试获取 Intent 的 id,但这很困难,因为每次 intentservice 启动时,保存 Intent id 的变量都是空的。这是我的 Intent 服务代码:

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 方法不可能实现这一点,因为它没有提供在它已经进行时中断它的方法。

我可以想到几个选项,您可以尝试看看它们是否符合您的需要。

  1. 复制 IntentService 代码以对其进行您自己的修改,从而允许您删除待处理的消息。看起来有人在这里取得了一些成功:Android: intentservice, how abort or skip a task in the handleintent queue
  2. 无需复制所有 IntentService 代码,您还可以像普通服务一样绑定(bind)到它(因为 IntentService 扩展了服务),这样您就可以编写自己的函数来删除未决消息。该链接中也提到了这个。
  3. 改为将 IntentService 重写为常规服务。使用此选项,您可以更好地控制添加和删除消息。

我在使用 IntentService 时遇到了类似的情况,最终我只是将其转换为服务。这让我可以同时运行这些任务,并在需要清除它们时取消它们。

关于android - 如何取消所有排队等待 Intent 服务的未决 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8358190/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com