如何使用“goAsync"对于广播接收器? [英] How to use "goAsync" for broadcastReceiver?

查看:31
本文介绍了如何使用“goAsync"对于广播接收器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Starting with Honeycomb (API 11) , Android has a feature to allow the broadcastReceiver run in an async way, providing it about 10 seconds before it assumes it can kill its process, using a method called "goAsync" :

这可以由应用程序在 onReceive(Context, Intent) 中调用以允许它在返回后保持广播处于活动状态功能.这并没有改变相对的期望响应广播(在 10 秒内完成),但确实允许将与其相关的工作转移到另一个线程的实现避免由于磁盘 IO 导致主 UI 线程出现故障.

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.

问题

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

The problem

I've searched in many places and didn't find any sample or tutorial of how to use it.

不仅如此,该方法还返回一个 PendingIntent 实例,我不确定如何处理它:

Not only that, but the method returns a PendingIntent instance which I'm not sure what to do with it:

返回一个表示结果的 BroadcastReceiver.PendingResult活跃的广播.BroadcastRecord 本身不再活跃;所有数据和其他交互都必须经过BroadcastReceiver.PendingResult API.PendingResult.finish()方法必须在广播处理完成后调用.

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.

问题

你如何使用这种方法?

The question

How do you use this method?

它返回的PendingIntent是什么,我该怎么处理?

What is the PendingIntent that is returned by it, and what should I do with it?

推荐答案

您可以找到简短的说明 此处.

You can find short explanation here.

如果您想将 BroadcastReceiveronReceive() 方法内部的处理移交给另一个线程,请使用 goAsync().onReceive() 方法然后可以在那里完成.PendingResult 被传递给新线程,你必须调用 PendingResult.finish() 来实际通知系统这个接收器可以被回收.

Use goAsync() if you want to handoff the processing inside of your BroadcastReceiver's onReceive() method to another thread. The onReceive() method can then be finished there. The PendingResult is passed to the new thread and you have to call PendingResult.finish() to actually inform the system that this receiver can be recycled.

例如:

final PendingResult result = goAsync();
Thread thread = new Thread() {
   public void run() {
      int i;
      // Do processing
      result.setResultCode(i);
      result.finish();
   }
};
thread.start();

这篇关于如何使用“goAsync"对于广播接收器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆