如何取消所有排队等待意图服务的待处理意图

发布于 2024-12-19 14:08:02 字数 1188 浏览 4 评论 0原文

我有一个意图服务,它会被用户和我的应用程序自动查询。我需要能够终止用户注销我的应用程序时查询的所有待处理意图,但我似乎无法让它发挥作用。我尝试过 stopService() 和 stopself(),但在用户注销后,意图继续触发意图服务。我会尝试获取意图的 id,但这很困难,因为每次意图服务启动时,保存意图 id 的变量都是空的。这是我的意图服务代码:

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
}

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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

玻璃人 2024-12-26 14:08:02

不幸的是,我认为使用标准 IntentService 方法不可能实现这一点,因为它没有提供在它已经运行时中断它的方法。

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

  1. 复制 IntentService 代码以进行自己的修改,从而允许您删除待处理的消息。看起来有人在这里取得了一些成功:Android:intentservice,如何中止或跳过handleintent队列中的任务
  2. 您也许还可以像普通Service一样绑定到它,而不是复制所有IntentService代码(因为 IntentService 扩展了 Service),因此您可以编写自己的函数来删除待处理的消息。该链接中也提到了这一点。
  3. 将 IntentService 重写为常规服务。使用此选项,您可以更好地控制添加和删除消息。

我遇到过听起来类似的情况,我使用的是 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. 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.

被你宠の有点坏 2024-12-26 14:08:02

这里
我应该何时释放本机 (Android NDK) 句柄? 是具有方法 cancelQueue()HangAroundIntentService 类。
该类还具有

方法public static Intent MarkedAsCancelIntent(Intent Intent)

将 Intent 转换为取消 Intent 的

public static boolean isCancelIntent(Intent Intent)

该类基于开源的 Google 代码。

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.

写给空气的情书 2024-12-26 14:08:02

只是一个想法,但在你的 onhandleintent 中你可以有一个参数来检查应用程序是否正在运行,如果没有,则不运行代码?例子。 使用 static var

boolean appRunning;

在应用程序的启动中,您可以在意图的 onhandle 中

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

Next ,当您将 appRunning 设置为 false 时,在 onPause 或 onDestroy 活动之后,您可以将 onhandleintent 代码包装在布尔值中:只是一个想法

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文