Search code examples
androidandroid-broadcastreceiver

How to send the value from broadcast receiver to Already started Activity


I am new to android development, kindly help me to resolve my issue.

  1. To get the Battery Percentage I have done this program and able to toast the battery percent in Broadcast receiver

  2. The Problem I’m facing not able to get it in Main Activity Again.

  3. Because all the activity in my application needs to use the broadcast receiver for battery level

  4. So I have kept it as separate java class file instead of having it in Activity itself.

  5. How to send the value from broadcast receiver to dynamically registered main activity and where to get it the battery value

MainActivity

public class MainActivity extends AppCompatActivity {
public static final String BATTERY_BROADCAST_ACTION = Intent.ACTION_BATTERY_CHANGED;
Context context;
private IntentFilter mIntentFilter;
private BroadcastReceiver myReceiver;
TextView txtBattery;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button btn = (Button) findViewById(R.id.btnCall);
    txtBattery = (TextView) findViewById(R.id.txtBattery);
    myReceiver = new MyReceiver();
    registerReceiver(myReceiver, new 
    IntentFilter(BATTERY_BROADCAST_ACTION));
}}

BroadCast Receiver

public class MyReceiver extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {
    // TODO: This method is called when the BroadcastReceiver is receiving
    // an Intent broadcast.
    Bundle extras = intent.getExtras();
    int level = extras.getInt(BatteryManager.EXTRA_LEVEL);
    Toast.makeText(context,"Battery = " + level,Toast.LENGTH_SHORT).show();
}}

Solution

  • You can employ event bus system to solve your problem. You can use EventBus

    First, create the Event for battery level changes:

    public class BatteryLevelEvent {
      private int batteryLevel;
    
      public BatteryLevelEvent(int batteryLevel) {
        this.batteryLevel = batteryLevel;
      }
    
      public int getBatteryLevel() {
        return this.batteryLevel;
      }
    }
    

    Second, whenever there is a battery level change, post the Event:

    public class MyReceiver extends BroadcastReceiver {
      ...
      @Override
      public void onReceive(Context context, Intent intent) {
        Bundle extras = intent.getExtras();
        int level = extras.getInt(BatteryManager.EXTRA_LEVEL);
        EventBus.getDefault().post(new BatteryLevelEvent(level));
      }
    }
    

    Now, you need to receive the Event. Each activity that want to receive the event need to be registered first. You can register it by something like this:

    public class YourActivity extends AppCompatActivity {
      ...
    
      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_layout);
        ...
        EventBus.getDefault().register(this);
      }
    
      // need to unregister when activity is not in foreground.
      @Override
      public void onPause() {
       super.onPause();
       EventBus.getDefault().unregister(this);
      }
    }
    

    Last, you need to subscribe to receive the Event:

    public class YourActivity extends AppCompatActivity {
      ...
    
      @Subscribe
      public void onMessageEvent(BatteryLevelEvent event) {
        int batteryLevel = event.getBatteryLevel();
        // do something with the battery level here.
      }
    }
    

    If you want to receive the Event in all the activity, you can create a base class activity with the register, unregister, and subscribe above. Then you can derive all you activity from that base class.