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);
}
}
分享到:
相关推荐
11.四大组件之BroadcastReceiver.pptx
Android程序技术:BroadcastReceiver.pptx
Android提高第六篇之BroadcastReceiver.docx
用 RxJava 实现 BroadcastReceiver 。.zip,Reactive Bindings for BroadcastReceivers — WIP
Android中Broadcast Receiver组件详解示例代码
初中级Android开发社招面试之Service及BroadcastReceiver
初中级Android开发社招面试之Service及BroadcastReceiver
android的broadcastReceiver的介绍。
BroadcastReceiver的详细介绍和demo例子,有很详细的代码解释说明,比较适合初学者,使用的是kotlin编写,大家学习请下载
一个Activity与Service的广播,
android 静态注册和动态注册的使用,普通广播的发送demo
使用人群:BroadcastReceiver初学者。 里面涉及BroadcastReceiver的静态注册(无序广播)、动态注册(无序广播)、有序广播。 项目对应:https://shuaici.blog.csdn.net/article/details/118337894
Android下BroadcastReceiver的简单示例。详见博客:http://www.cnblogs.com/plokmju/p/android_BroadcastReceiver.html
这是一个国外的android手机应用源码实例,BroadcastReceiver这个应用我还没用过,不知道是干什么的,源码可为学习安卓开发的做些参考吧。
Android高级应用源码-音乐播放器消息通知栏切歌栏效果(Notification+BroadcastReceiver).zip
android服务自启动Service以及BroadcastReceiver和Notification.zip
Android开机自启动, <receiver android:name=".broadcastReceiver.BootCompletedReceiver">
Android中BroadcastReceiver的定义.pdf 学习资料 复习资料 教学资源