Skip to content Skip to sidebar Skip to footer

Passing Data From Broadcastreceiver To Service

My goal is to create a method that gets the data from broadcast receiver. I have a service that register a BroadcastReceiver to monitor battery plug and unplug. How can i get data

Solution 1:

In receiver, you can just put all params into a Bundle, and use startService(Intent) containing that bundle to call the target service. In service, you parse the bundle and get all params.

Solution 2:

In broadcast receiver,

Intentintent=newIntent(Constants.BROADCAST_DETECTED_ACTIVITY);
   // Data you need to pass to activity
   intent.putExtra("type",Type);
   context.sendBroadcast(intent);

In Service,

registerReceiver(broadcastReceiver, newIntentFilter(Constants.BROADCAST_DETECTED_ACTIVITY));

BroadcastReceiverbroadcastReceiver=newBroadcastReceiver() {
        @OverridepublicvoidonReceive(Context context, Intent intent) {
            if (intent.getAction().equals(BROADCAST_DETECTED_ACTIVITY)) {
        inttype= intent.getIntExtra("type", 0);
      }
    }
};   

Post a Comment for "Passing Data From Broadcastreceiver To Service"