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
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.
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.
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 } });
I suggest you use a Handler.
handler = new Handler()
post()
method of your Handler instance in the onReceive()
method to submit the Runnable that updates the UIThis 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(); } }); } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With