I am learning Android
concepts Activity
and BroadCastReceiver
. I want to update the content of Activity
from the BroadtCastReceiver
both are in different java class.
It is something like
MyActivity.java
and MyBroadtCastReceiver.java
Is this possible to do this in Android ?
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. xml. In the above code, we have taken text view, when user gets the data from intent service it will update.
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 } });
How can we transfer data from broadcast receiver to activity? Code. Open your project where you want to implement this. Open your BroadcastReceiver class from where you pass data to activity inside your onReceive() you need to start intent and pass data inside intent and start sendBroadcast() as shown bellow.
Following are the two arguments of the onReceive() method: Context: This is used to access additional information, or to start services or activities. Intent: The Intent object is used to register the receiver.
A BroadcastReceiver
can be used in many ways but when it comes to something as specific as updating the UI components of an Activity
, there is little advantage to declaring / defining a BroadcastReceiver
in it's own Java class file.
Reasoning - the BroadcastReceiver
has to have some prior "knowledge" of the Activity
and what it is required to do in order to update the UI. In effect the BroadcastReceiver
is tied to the Activity
itself and it makes sense to declare / define it as an inner class.
Another important aspect is the Activity
needs to be in a "running" (i.e., visible) state in order to guarantee manipulation of UI components. In this case, registering the receiver in onResume()
and unregistering in onPause()
will help prevent problems.
Using a generic template I'd do something like the following...
class MyActivity extends Activity {
boolean mIsReceiverRegistered = false;
MyBroadcastReceiver mReceiver = null;
// onCreate(...) here
@Override
protected void onResume() {
// Other onResume() code here
if (!mIsReceiverRegistered) {
if (mReceiver == null)
mReceiver = new MyBroadcastReceiver();
registerReceiver(mReceiver, new IntentFilter("YourIntentAction"));
mIsReceiverRegistered = true;
}
}
@Override
protected void onPause() {
if (mIsReceiverRegistered) {
unregisterReceiver(mReceiver);
mReceiver = null;
mIsReceiverRegistered = false;
}
// Other onPause() code here
}
private void updateUI(Intent intent) {
// Do what you need to do
}
private class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
updateUI(intent);
}
}
}
EDIT: A couple of extra notes...
BroadcastReceiver
is between entering and leaving onReceive(...)
. Once it has returned from onReceive(...)
the instance remains in a dormant state waiting for the next broadcast.BroadcastReceiver
isn't designed for "heavy lifting". Basically the onReceive(...)
method should be kept as simple as possible. Any methods it calls should also be as light-weight as possible...get in, do your stuff, get out then wait for the next broadcast. If updating the UI is going to take some time (perhaps updating a ListView
by re-querying a database for a large amount of data for example), consider calling code which performs asynchronously (an AsyncTask
for example).Yes its possible. This is what i do. Class i send the broadcast from (BackgroundActivity.java):
public static final String BROADCAST_BUFFER_SEND_CODE = "com.example.SEND_CODE";
onCreate(){
bufferIntentSendCode = new Intent(BROADCAST_BUFFER_SEND_CODE);
}
private void sendBufferingBroadcastSendCode() {
bufferIntentSendCode.putExtra("buffering", "1");
sendBroadcast(bufferIntentSendCode);
}
The class it will receive the broadcast(SendCode.java):
onResume(){
registerReceiver(broadcastBufferReceiver, new IntentFilter(BackgroundActivity.BROADCAST_BUFFER_SEND_CODE));
}
// set up broadcast receiver
private BroadcastReceiver broadcastBufferReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent bufferIntent) {
SendCode.this.LoadMessages(alarmNumber);
}
};
I unregister it in onPause
this.unregisterReceiver(broadcastBufferReceiver);
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