我有一个使用广播接收器执行BOOT_COMPLETED动作的应用程序。我在goAsync内部使用它来执行一些异步任务,当获得有关末尾的异步信息时,我将调用PendingResult :: finish方法。
@Override
public void onReceive(Context context, Intent intent) {
log.i("SystemBootReceiver received intent");
if (!Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
log.w("Incorrect boot action");
return;
}
final PendingResult pendingResult = goAsync();
AsyncTask<Void, Integer, Void> asyncTask = new ReceiverTask(pendingResult, context, log);
asyncTask.execute();
log.i("SystemBootReceiver onReceive end");
}
private static class ReceiverTask extends AsyncTask<Void, Integer, Void> {
private final PendingResult pendingResult;
private final Context context;
private final Logger log;
private ReceiverTask(PendingResult pendingResult, Context context, Logger log) {
this.pendingResult = pendingResult;
this.context = context.getApplicationContext();
this.log = log;
}
@Override
protected Void doInBackground(Void... params) {
new EventEnvironment(context, env -> {
log.i("SystemBootReceiver async end");
pendingResult.finish();
}, EnvironmentMode.RegisterServices, "Boot", null);
return null;
}
@Override
protected void onPostExecute(Void result) {
log.i("SystemBootReceiver onPostExecute");
}
}
我从后台线程(AsyncTask)使用的某些服务在主线程上发布结果。
行代码:
log.i("SystemBootReceiver async end");
在主线程上的代码之后调用:
log.i("SystemBootReceiver onPostExecute");
我的问题是: 我可以安全地假定使用go async时不会杀死主线程吗? 我认为可以在其中使用异步任务是安全的,但是我无法在网上找到明确的信息。