Android 如何取消为意向服务请求的所有待定意向
我有一个intentservice,用户和我的应用程序会自动询问它。当用户从我的应用程序中注销时,我需要能够杀死所有被请求的挂起意图,但我似乎无法让它工作。我尝试了stopService()和stopself(),但在用户注销后,intent会继续触发intentservice。我会尝试获取intent的id,但这很困难,因为每次intentservice启动时,保存intent id的变量都是空的。这是我的intentservice代码:Android 如何取消为意向服务请求的所有待定意向,android,kill,intentservice,Android,Kill,Intentservice,我有一个intentservice,用户和我的应用程序会自动询问它。当用户从我的应用程序中注销时,我需要能够杀死所有被请求的挂起意图,但我似乎无法让它工作。我尝试了stopService()和stopself(),但在用户注销后,intent会继续触发intentservice。我会尝试获取intent的id,但这很困难,因为每次intentservice启动时,保存intent id的变量都是空的。这是我的intentservice代码: public class MainUploadInte
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方法是不可能实现这一点的,因为它不提供在它运行时中断它的方法 我可以想出一些选择,你可以试着看看它们是否适合你的需要
cancelQueue()
的hangaroundentservice
类。
这个类也有这个方法
publicstaticintent标记为扫描意图(Intent Intent)
将意图转换为取消意图,以及
公共静态布尔值iscancelint(意图)
该类基于开源的Google代码。只是一个想法,但在你的OnHandleContent中,你能有一个参数来检查应用程序是否正在运行,如果没有,那么就不要运行代码吗?例子。在你的应用程序的开始,你可以有一个静态变量
boolean appRunning;
接下来,在意图的onhandle中,当您将appRunning设置为false时,在活动的onPause或onDestroy之后,您可以将onhandleintent代码包装为布尔值:
protected void onHandleIntent(final Intent intent) {
if(MainActivity.appRunning){
...
}
}
只是一个想法android开发者似乎无法使用intent Que和IntentService的实际意图。我将把它标记为已修复,因为这是我所能想到的最佳答案,尽管我们决定采用更简单的不同方法。谢谢