android - 在销毁 BroadcastReceiver 警报之前等待 AsyncTask 完成
问题描述
我的 Android 应用程序在我设置的重复警报方面存在一些问题。AsyncTask 有时需要 5-15 秒才能完成,在较高的范围内,任务最终没有完成,因为 Android 系统onReceive
在任务完成之前最终在接收器类中结束。
有没有办法让我保持警报直到任务完成,或者这是不好的做法?
这是onReceive
我的意图类:
@Override
public void onReceive(Context context, Intent intent)
{
getPrefs(context);
color = (Color.parseColor("#FFFF4500"));
instances = 0;
backgroundExecute(context); //5-15 sec, sometimes doesn't get to finish
}
解决方案
So I did some reading on the BroadcastReceiver documentation on Google's page, and found that using goAsync()
in my onReceive would keep the broadcast alive until the result is returned by the AsyncTask.
Found here: https://developer.android.com/reference/android/content/BroadcastReceiver.html#goAsync()
推荐阅读
- html - 与内部 SVG 相比,为什么按钮高度上有额外的像素
- powershell - 替换代码签名证书后 PowerShell 模块更新失败
- javascript - 在数据链中使用空格
- r - 拟合具有不同类别数量的潜在轨迹模型并将模型保存在列表中
- javascript - 发送 dropzone 以及表单中的其他数据,而不指定每个输入字段
- javascript - 获取 UnhandledPromiseRejectionWarning:RangeError:map 函数超出了最大调用堆栈大小
- javascript - 有没有一种解决方案,我不必在 onepager 上微调我的边距和填充?
- swift - 毒蛇/CleanSwift。将跨多个视图使用的可重用业务逻辑放在哪里?
- mysql - PHP-MySQL - 如果表字段不包含值/空,则销毁/忽略表字段
- cloud-foundry - Cloud Foundry 通过 curl 为一个应用提供超过 10 个端口