http://developer.android.com/reference/android/content/BroadcastReceiver.PendingResult.html
State for a result that is pending for a broadcast receiver. Returned by goAsync() while in BroadcastReceiver.onReceive(). This allows you to return from onReceive() without having the broadcast terminate; you must call finish() once you are done with the broadcast. This allows you to process the broadcast off of the main thread of your app.
Note on threading: the state inside of this class is not itself thread-safe, however you can use it from any thread if you properly sure that you do not have races. Typically this means you will hand the entire object to another thread, which will be solely responsible for setting any results and finally calling finish().
http://www.vogella.com/tutorials/AndroidBroadcastReceiver/article.html#broadcastreceiver_asynchronousprocessing
1.4. Asynchronous processing
Before API level 11, you could not perform any asynchronous operation in the onReceive() method, because once the onReceive() method had been finished, the Android system was allowed to recycle that component. If you have potentially long running operations, you should trigger a service instead.
Since Android API 11 you can call the goAsync() method. This method returns an object of the PendingResult type. The Android system considers the receiver as alive until you call the PendingResult.finish() on this object. With this option you can trigger asynchronous processing in a receiver. As soon as that thread has completed, its task calls finish() to indicate to the Android system that this component can be recycled.
private static Handler sAsyncHandler;
static {
final HandlerThread thread = new HandlerThread("DownloadReceiver");
thread.start();
sAsyncHandler = new Handler(thread.getLooper());
}
if (Intent.ACTION_UID_REMOVED.equals(action)) {
final PendingResult result = goAsync();
sAsyncHandler.post(new Runnable() {
@Override
public void run() {
handleUidRemoved(context, intent);
result.finish();
}
});
private void handleUidRemoved(Context context, Intent intent) {
final ContentResolver resolver = context.getContentResolver();
final int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
final int count = resolver.delete(
Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI, Constants.UID + "=" + uid, null);
if (count > 0) {
Slog.d(TAG, "Deleted " + count + " downloads owned by UID " + uid);
}
}
分享到:
相关推荐
Android程序技术:BroadcastReceiver.pptx
在Android系统中,BroadcastReceiver(广播接收者)是四大组件之一,它负责监听并响应系统或应用程序发布的广播意图(Intent)。BroadcastReceiver可以帮助我们的应用在不运行时也能接收到消息,从而实现后台运行的...
BroadcastReceiver是Android系统四大组件之一,它主要用于在应用程序的不同组件或者不同应用程序之间进行异步通信。在Android系统中,BroadcastReceiver扮演着消息分发者的角色,它可以接收并响应系统或应用自行发送...
在Android开发中,BroadcastReceiver是系统广播事件的重要接收者,用于监听并响应系统或应用程序的广播消息。然而,传统的BroadcastReceiver实现方式存在一些限制,如不易于测试、生命周期管理复杂等。RxJava作为一...
在"BroadcastReceiver.rar"这个压缩包中,包含的项目很可能是使用Delphi的FireMonkey(FMX)框架开发的一个Android应用示例。Delphi_FMX是Embarcadero公司提供的跨平台UI库,它允许开发者使用相同的代码库在多个操作...
在Android应用开发中,Service和BroadcastReceiver是两个非常重要的组件,它们构成了应用程序后台运行和系统消息传递的基础。本文将深入探讨这两个组件的概念、用途、工作原理以及面试中可能遇到的相关问题。 **一...
Service是Android系统中的一种组件,主要用于在后台执行长时间运行的操作,而BroadcastReceiver则是接收系统或应用程序广播消息的组件。在Android开发中,理解和熟练运用Service和BroadcastReceiver是至关重要的。 ...
在"BroadcastReceiver.zip"这个文件中,我们可以推测它包含了一个BroadcastReceiver的相关示例代码。 首先,BroadcastReceiver的注册有两种方式:静态注册和动态注册。静态注册是在AndroidManifest.xml文件中声明,...
### 广播接收器(BroadcastReceiver)在Android中的应用与管理 #### 概念 `BroadcastReceiver`作为Android四大组件之一,在系统与应用之间扮演着关键的信息传递角色。不同于Activity等具有用户界面的组件,`...
在Android系统中,四大组件是应用程序的核心组成部分,包括Activity、Service、Content Provider和BroadcastReceiver。本篇文章将聚焦于BroadcastReceiver,讲解如何在Android应用中使用它,特别是静态注册和动态...
【BroadcastReceiver源码解析】 BroadcastReceiver是Android系统中的一个重要组件,它是Android应用程序接收系统广播事件的主要途径。在Android系统中,任何全局性、瞬时性的事件,如网络连接状态变化、系统启动...
使用人群:BroadcastReceiver初学者。 里面涉及BroadcastReceiver的静态注册(无序广播)、动态注册(无序广播)、有序广播。 项目对应:https://shuaici.blog.csdn.net/article/details/118337894
这个“音乐播放器消息通知栏切歌栏效果”项目就是针对这一需求的实例,它巧妙地结合了Notification和BroadcastReceiver这两个关键组件。下面将详细解析这个项目的知识点。 首先,`Notification`是Android系统提供的...
在Android开发中,服务(Service)、广播接收器(BroadcastReceiver)和通知(Notification)是三个核心组件,它们各自承担着不同的职责,同时在特定场景下相互配合,为应用程序提供后台运行、事件监听和用户交互的...
在Android系统中,BroadcastReceiver(广播...为了更深入地了解BroadcastReceiver,建议阅读官方文档和其他开发者分享的经验,如博客http://www.cnblogs.com/plokmju/p/android_BroadcastReceiver.html中的详细解释。
在Android操作系统中,BroadcastReceiver是一个核心组件,它扮演着接收系统或应用程序广播消息的角色。广播接收器允许应用程序对系统事件做出响应,即使应用并未在前台运行。BroadcastReceiver的使用极大地扩展了...
在描述中提到的`<receiver android:name=".broadcastReceiver.BootCompletedReceiver">`,这是AndroidManifest.xml文件中的一个组件声明,用于注册BroadcastReceiver。`.broadcastReceiver.BootCompletedReceiver`是...