android – 如何使用“goAsync”为broadcastReceiver?

前端之家收集整理的这篇文章主要介绍了android – 如何使用“goAsync”为broadcastReceiver?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
背景

从Honeycomb(API 11)开始,Android具有允许broadcastReceiver以异步方式运行的功能,在它假定可以使用“goAsync”的方法杀死其进程之前约10秒提供:

This can be called by an application in onReceive(Context,Intent) to
allow it to keep the broadcast active after returning from that
function. This does not change the expectation of being relatively
responsive to the broadcast (finishing it within 10s),but does allow
the implementation to move work related to it over to another thread
to avoid glitching the main UI thread due to disk IO.

问题

我在许多地方搜索过,没有找到任何示例或教程如何使用它.

不仅如此,但是该方法返回一个PendingIntent实例,我不知道该怎么办

Returns a BroadcastReceiver.PendingResult representing the result of
the active broadcast. The BroadcastRecord itself is no longer active;
all data and other interaction must go through
BroadcastReceiver.PendingResult APIs. The PendingResult.finish()
method must be called once processing of the broadcast is done.

问题

你如何使用这种方法

它返回的是什么是PendingIntent,该怎么办?

解决方法

你可以找到简短的说明 here

如果要将BroadcastReceiver的onReceive方法中的处理切换到另一个线程,请使用goAsync. onReceive方法可以在那里完成. PendingResult被传递给新的线程,你必须调用PendingResult.finish()来实际通知系统这个接收器可以被回收.

例如

final PendingResult result = goAsync();
Thread thread = new Thread() {
   public void run() {
      int i;
      // Do processing
      result.setResultCode(i);
      result.finish();
   }
};
thread.start();
原文链接:https://www.f2er.com/android/311647.html

猜你在找的Android相关文章