如何取消所有排队等待意图服务的待处理意图
我有一个意图服务,它会被用户和我的应用程序自动查询。我需要能够终止用户注销我的应用程序时查询的所有待处理意图,但我似乎无法让它发挥作用。我尝试过 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(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.
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.
这里
我应该何时释放本机 (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 methodcancelQueue()
.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.
只是一个想法,但在你的 onhandleintent 中你可以有一个参数来检查应用程序是否正在运行,如果没有,则不运行代码?例子。 使用 static var
在应用程序的启动中,您可以在意图的 onhandle 中
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
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:
Just a thought