[英]Background process (AsyncTask) still running after app closes
My app has 3 or 4 async tasks, two of which get data from an sqlite database and create intents to send it to a receiver in the main activity.我的应用程序有 3 或 4 个异步任务,其中两个从 sqlite 数据库中获取数据,并创建意图将其发送到主活动中的接收器。 It all works fine - until I close the app and then restart it.
一切正常 - 直到我关闭应用程序然后重新启动它。 The data is then not received correctly.
然后无法正确接收数据。 After a lot of experimentation I discovered that if, after closing the app normally, I 'force stop' it, it works fine again - until the next time I run the task and again close the app.
经过大量实验后,我发现如果在正常关闭应用程序后“强制停止”它,它会再次正常工作 - 直到我下次运行任务并再次关闭应用程序。 This behaviour doesn't show itself normally when running on the debugger.
在调试器上运行时,此行为不会正常显示。 However, if I start the app on the device (rather than via Android Studio) and then attach the debugger, I can see quite clearly that the receiver is receiving two copies of the broadcast each time, instead of only one.
但是,如果我在设备上启动应用程序(而不是通过 Android Studio)然后附加调试器,我可以很清楚地看到接收器每次都接收到两个广播副本,而不是只有一个。 One copy is correct, the other contains rubbish data.
一份是正确的,另一份包含垃圾数据。 I have addded extra lines which show that the task ID is the same in each case.
我添加了额外的行,显示任务 ID 在每种情况下都是相同的。
The sending intent looks like this发送意图如下所示
private void sendRoads(ArrayList<RedRoad> roadsChunk)
{
ArrayList<RedRoad> theseRoads = new ArrayList<>(roadsChunk);
Intent mapIntent = new Intent("newRoads");
mapIntent.putParcelableArrayListExtra("newRoads", theseRoads);
mapIntent.putExtra("chunkIndex",cIndex++);
int threadID = android.os.Process.getThreadPriority(android.os.Process.myTid());
mapIntent.putExtra("threadID",threadID);
LocalBroadcastManager.getInstance(ctx).sendBroadcast(mapIntent);
// get ready for next group while we show these
roadsChunk.clear();
}
In order to try to solve this I have added extra lines to onDestroy:为了尝试解决这个问题,我在 onDestroy 中添加了额外的行:
@Override
public void onDestroy() {
super.onDestroy();
if (roadCreation!=null)
roadCreation.cancel(true);
if (parseKmlFile != null)
parseKmlFile.cancel(true);
Intent serviceIntent = new Intent(getApplicationContext(), LocationService.class);
stopService(serviceIntent);
}
but that appears to have made no difference (the two classes named are the two which use the method above. As you can see, there is also a service involved but I can tell by other means that it is stopping correctly).但这似乎没有任何区别(命名的两个类是使用上述方法的两个类。如您所见,还涉及一项服务,但我可以通过其他方式判断它正在正确停止)。
I have tried to catch the erroneous broadcasts and ignore any action on them, but they still mess up the correct data.我试图捕捉错误的广播并忽略对它们的任何操作,但它们仍然会弄乱正确的数据。
I have two test devices;我有两个测试设备; one sdk 26 (Oreo) and one 29 (Q);
一个 sdk 26(奥利奥)和一个 29(Q); the latter is showing this problem evry time, the former only occasionally.
后者每次都显示这个问题,前者只是偶尔出现。
Thanks to this answer I have solved this at last.感谢这个答案,我终于解决了这个问题。 My onDestroy() now look like this:
我的 onDestroy() 现在看起来像这样:
public void onDestroy() {
super.onDestroy();
Intent serviceIntent = new Intent(getApplicationContext(), LocationService.class);
stopService(serviceIntent);
// ensure all background tasks stop correctly
int id= android.os.Process.myPid();
android.os.Process.killProcess(id);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.