Register a broadcast receiver from a service in a new stream

I have a broadcast that starts a lengthy operation (boot process). In the service code launched from the Activity class, I need to register this receiver in a new thread.

I checked this post Are Android BroadcastReceivers running in a new thread? but I need a more specific example of using Context.registerReceiver (BroadcastReceiver receiver, IntentFilter filter, String broadcastPermission, handler scheduler)

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

Many thanks. RA

+3
source share
1

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);
// Register the broadcast receiver to run on the separate Thread
registerReceiver (myReceiver, intentFilter, broadcastPermission, handler);
+12

All Articles