【问题标题】:Waiting for AsyncTask to finish before destroying BroadcastReceiver Alarm在销毁 BroadcastReceiver 警报之前等待 AsyncTask 完成
【发布时间】:2018-10-14 10:38:46
【问题描述】:

我的 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

}

【问题讨论】:

    标签: android android-asynctask broadcastreceiver


    【解决方案1】:

    因此,我阅读了 Google 页面上的 BroadcastReceiver 文档,发现在我的 onReceive 中使用 goAsync() 将使广播保持活动状态,直到 AsyncTask 返回结果。

    在这里找到:https://developer.android.com/reference/android/content/BroadcastReceiver.html#goAsync()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多