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();

相关文章

这篇“android轻量级无侵入式管理数据库自动升级组件怎么实现...
今天小编给大家分享一下Android实现自定义圆形进度条的常用方...
这篇文章主要讲解了“Android如何解决字符对齐问题”,文中的...
这篇文章主要介绍“Android岛屿数量算法怎么使用”的相关知识...
本篇内容主要讲解“Android如何开发MQTT协议的模型及通信”,...
本文小编为大家详细介绍“Android数据压缩的方法是什么”,内...