Accessing the UI Thread Handler from a Service

I am trying something new on Android for which I need to access the UI thread handler.

I know the following:

  • The user interface thread has its own handler and looper
  • Any message will be sent to the message queue in the thread UI
  • Petler takes the event and passed it to the handler
  • The handler processes the message and sends a specfic event to the user interface

I want to have my own service, which should receive a UI thread handler and put a message in this handler. So this message will be processed and will be issued to the user interface. Here, the service will be a regular service that will be launched using an application.

I would like to know if this is possible. If so, suggest some code snippets so I can try.

Relationship Girish

+75
android multithreading user-interface handler service
Jun 16 2018-11-11T00:
source share
5 answers

This piece of code creates a handler associated with the main (UI) thread:

Handler handler = new Handler(Looper.getMainLooper()); 

Then you can publish material for execution in the main thread (UI), for example:

 handler.post(runnable_to_call_from_main_thread); 

If the handler itself is created from the main (UI) stream, the argument can be omitted for brevity:

 Handler handler = new Handler(); 

The Android Developer's Guide on Processes and Threads contains more information.

+153
Jul 08 '11 at 20:21
source share

Create a Messenger object attached to your Handler and pass the Messenger to the Service (for example, Intent extra for startService() ). Service can then send a Message to Handler via Messenger . Here is an example application demonstrating this.

+27
Jun 16 '11 at 11:50
source share

At the moment, I prefer to use an event library such as Otto for this problem. Just sign the necessary components (activity):

 protected void onResume() { super.onResume(); bus.register(this); } 

Then specify the callback method:

 public void onTimeLeftEvent(TimeLeftEvent ev) { // process event.. } 

and then when your service executes this statement:

 bus.post(new TimeLeftEvent(340)); 

This POJO will be transferred to your above action and all other subscription components. Simple and elegant.

+4
Sep 28 '13 at 12:57 on
source share

You can receive values ​​through the broadcast receiver ...... as follows: first create your own IntentFilter as,

 Intent intentFilter=new IntentFilter(); intentFilter.addAction("YOUR_INTENT_FILTER"); 

Then create the BroadcastReceiver inner class as,

  private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { /** Receives the broadcast that has been fired */ @Override public void onReceive(Context context, Intent intent) { if(intent.getAction()=="YOUR_INTENT_FILTER"){ //HERE YOU WILL GET VALUES FROM BROADCAST THROUGH INTENT EDIT YOUR TEXTVIEW/////////// String receivedValue=intent.getStringExtra("KEY"); } } }; 

Now register your broadcast receiver in onResume () as,

 registerReceiver(broadcastReceiver, intentFilter); 

And finally, Unregister BroadcastReceiver in onDestroy () like,

 unregisterReceiver(broadcastReceiver); 

Now the most important part ... You need to start broadcasting to where you need to send the values ​​..... like so,

 Intent i=new Intent(); i.setAction("YOUR_INTENT_FILTER"); i.putExtra("KEY", "YOUR_VALUE"); sendBroadcast(i); 

.... greetings :)

+2
Mar 21 '14 at 9:36
source share

Decision:

  • Create a Handler with Looper from the main topic: requestHandler
  • Create a Handler with a Looper from the main thread: responseHandler and override the handleMessage method
  • send Runnable task on requestHandler
  • Inside the Runnable task, call sendMessage for responseHandler
  • This sendMessage calls handleMessage on the responseHandler.
  • Get attributes from Message and process it, update interface

Code example:

  /* Handler from UI Thread to send request */ Handler requestHandler = new Handler(Looper.getMainLooper()); /* Handler from UI Thread to process messages */ final Handler responseHandler = new Handler(Looper.getMainLooper()) { @Override public void handleMessage(Message msg) { /* Processing handleMessage */ Toast.makeText(MainActivity.this, "Runnable completed with result:"+(String)msg.obj, Toast.LENGTH_LONG) .show(); } }; for ( int i=0; i<10; i++) { Runnable myRunnable = new Runnable() { @Override public void run() { try { /* Send an Event to UI Thread through message. Add business logic and prepare message by replacing example code */ String text = "" + (++rId); Message msg = new Message(); msg.obj = text.toString(); responseHandler.sendMessage(msg); System.out.println(text.toString()); } catch (Exception err) { err.printStackTrace(); } } }; requestHandler.post(myRunnable); } 
0
Aug 28 '17 at 17:01
source share



All Articles