注册从服务的广播接收机在一个新的线程 [英] Register a broadcast receiver from a service in a new thread

查看:305
本文介绍了注册从服务的广播接收机在一个新的线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个broadcastreciever它开始长操作(上传过程)。 在服务的code从活动类开始,我需要在一个新线程注册此接收器。

I have a broadcastreciever which start a long operation (uploading process). In the code of a service started from the Activity class, I need to register this receiver in a new thread.

我已经检查了这个帖子<一个href="http://stackoverflow.com/questions/5394950/are-android-broadcastreceiver-started-in-a-new-thread">Are安卓的BroadcastReceiver开始在一个新线程?但我需要有关使用Context.registerReceiver一个更具体的例子(BroadcastReceiver的接收器,IntentFilter的过滤器,串broadcastPermission,处理器调度)

I have checked this post Are Android BroadcastReceiver started in a new thread? but I need a more concrete example about using Context.registerReceiver(BroadcastReceiver receiver, IntentFilter filter, String broadcastPermission, Handler scheduler)

其实我需要知道如何从服务中创建一个新的线程,并登记接收器和连接到该线程。

Actually I need to know how to create a new thread from a service and to register the receiver and attached to this thread.

非常感谢你。 RA

推荐答案

在你的服务的的onCreate()

private Handler handler; // Handler for the separate Thread

HandlerThread handlerThread = new HandlerThread("MyNewThread");
handlerThread.start();
// Now get the Looper from the HandlerThread so that we can create a Handler that is attached to
//  the HandlerThread
// NOTE: This call will block until the HandlerThread gets control and initializes its Looper
Looper looper = handlerThread.getLooper();
// Create a handler for the service
handler = new Handler(looper, this);
// Register the broadcast receiver to run on the separate Thread
registerReceiver (myReceiver, intentFilter, broadcastPermission, handler);

这篇关于注册从服务的广播接收机在一个新的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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