Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update UI in a BroadcastReceiver

I created an application in which I registered a broadcast receiver within my main class(Main Activity) and whenever I receive something in my BroadcastReceiver I want to update UI for e.g. I want to show an alert box or set some text view of my MainActivity. I receive all the values in my receiver but unable to set them, can somebody help me so that I can update my UI in the BroadcastReceiver.

My BroadcastReceiver class is inner class of MainActivity like this :-

public class MainActivity extends Activity {     ..........  public static class NissanTabBroadcast extends BroadcastReceiver {         @Override         public void onReceive(Context context, Intent intent) {             SharedPreferences shrd = context.getSharedPreferences("NissanGallery", context.MODE_WORLD_READABLE);             type = shrd.getString("type", "null");             badges = shrd.getString("badge_count", "null");              //badge_tips_text.setText(badges);             /*Editor edit =  shrd.edit();             edit.remove("type");*/              Toast.makeText(context, "" + type + "\n" + badge_tips_text.getText().toString(), Toast.LENGTH_LONG).show();         }     } } 

Any help will be appreciable

Thanks

like image 623
Salman Khan Avatar asked Feb 01 '13 09:02

Salman Khan


People also ask

Can we update UI from service?

This example demonstrate about How to update ui from Intent Service. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.

Which method is used to set the updates of UI?

Android Thread Updating the UI from a Background Thread The solution is to use the runOnUiThread() method, as it allows you to initiate code execution on the UI thread from a background Thread.

What are different ways of updating UI from background thread?

In this case, to update the UI from a background thread, you can create a handler attached to the UI thread, and then post an action as a Runnable : Handler handler = new Handler(Looper. getMainLooper()); handler. post(new Runnable() { @Override public void run() { // update the ui from here } });


1 Answers

I suggest you use a Handler.

  1. Initialize a Handler in the Activity, example: handler = new Handler()
  2. Provide the handler to the BroadcastReceiver in the constructor, in the same way as I did for NissanTabBroadcast above
  3. Use post() method of your Handler instance in the onReceive() method to submit the Runnable that updates the UI

This is the cleanest solution I can imagine.

public class MainActivity extends Activity {      private MyReceiver receiver;      @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          receiver = new MyReceiver(new Handler()); // Create the receiver         registerReceiver(receiver, new IntentFilter("some.action")); // Register receiver          sendBroadcast(new Intent("some.action")); // Send an example Intent     }      public static class MyReceiver extends BroadcastReceiver {          private final Handler handler; // Handler used to execute code on the UI thread          public MyReceiver(Handler handler) {             this.handler = handler;         }          @Override         public void onReceive(final Context context, Intent intent) {             // Post the UI updating code to our Handler             handler.post(new Runnable() {                 @Override                 public void run() {                     Toast.makeText(context, "Toast from broadcast receiver", Toast.LENGTH_SHORT).show();                 }             });         }     } } 
like image 187
allprog Avatar answered Sep 16 '22 15:09

allprog