从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,该怎么办?
解决方法@H_404_35@
你可以找到简短的说明
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();
如果要将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();