问题
How to read data from an activity in an broadcast receiver if the broadcast receiver is registered through the manifest file??? Please Help...
回答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 :
<receiver android:name=".YourReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
and in code you can use :
public class YourReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
{
//updateWidget();
SharedPreferences prfs = context.getSharedPreferences("defaul", 0);
boolean bb=prfs.getBoolean("eer",false);
alarm=context.getSharedPreferences("def", 0).getString("alarm_time","");
}
}
}
回答2:
If you are calling broadcast receiver thru code then u can call it like as below
Intent intent1 = new Intent(context, TimeAlarm.class)
intent1.putExtra("var1", "value");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
intent1, PendingIntent.FLAG_ONE_SHOT);
回答3:
Not getting value from activity to receiver class which extends Broadcast Receiver
Intent inte = new Intent();
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);
}
来源:https://stackoverflow.com/questions/7846338/how-to-pass-value-from-an-activity-in-an-broadcast-receiver