Skip to content Skip to sidebar Skip to footer

How To Pass Value From An Activity In An Broadcast Receiver?

How to read data from an activity in an broadcast receiver if the broadcast receiver is registered through the manifest file??? Please Help...

Solution 1:

First you need to store data in SharedPreferences and then get them into broadcast receiver.

You can use the code below in manifest file :

<receiverandroid:name=".YourReceiver"><intent-filter><actionandroid:name="android.intent.action.BOOT_COMPLETED" /></intent-filter></receiver>

and in code you can use :

publicclassYourReceiverextendsBroadcastReceiver {

    @OverridepublicvoidonReceive(Context context, Intent intent) {
        if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
        {
            //updateWidget();SharedPreferencesprfs= context.getSharedPreferences("defaul", 0);
            boolean bb=prfs.getBoolean("eer",false);
            alarm=context.getSharedPreferences("def", 0).getString("alarm_time","");
        }
    }
}

Solution 2:

If you are calling broadcast receiver thru code then u can call it like as below

Intentintent1=newIntent(context, TimeAlarm.class)
intent1.putExtra("var1", "value");
PendingIntentpendingIntent= PendingIntent.getBroadcast(context, 0,
             intent1, PendingIntent.FLAG_ONE_SHOT);

Solution 3:

Not getting value from activity to receiver class which extends Broadcast Receiver

Intentinte=newIntent();
    inte.setAction("MyBroadcast");

    inte.putExtra("Phone_number1", phoo1);
    inte.putExtra("Phone_number2",phoo2);
    inte.putExtra("Phone_number3", phoo3);
    sendBroadcast(inte);

Bundle extras = intent.getExtras();
        if (extras != null) {

          String phoo1 = (String) extras.get("Phone_number1");//getting null value
          String phoo2 = (String) extras.get("Phone_number2");
          String phoo3 = (String) extras.get("Phone_number3");
          System.out.println("Value Of Contact Numbers Outside intent in Reciver");
          System.out.println("Value Of First Contact Number"+phoo1);
          System.out.println("Value Of Second Contact Number"+phoo2);
          System.out.println("Value Of Third Contact Number"+phoo3);
}

Post a Comment for "How To Pass Value From An Activity In An Broadcast Receiver?"