gpt4 book ai didi

android - 如何将 "goAsync"用于广播接收器?

转载 作者:IT老高 更新时间:2023-10-28 23:19:34 25 4
gpt4 key购买 nike

背景

从 Honeycomb (API 11) 开始,Android 具有允许广播接收器以异步方式运行的功能,在假定它可以终止其进程之前提供大约 10 秒,使用称为“goAsync”的方法:

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 .

如果您想将 BroadcastReceiveronReceive() 方法内部的处理移交给另一个线程,请使用 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 - 如何将 "goAsync"用于广播接收器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22741202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com