当我从一个 Activity 发送数据时,它调用 BroadcastReceiver,我在 BroadcastReceiver 中收到 null。
这是我的 BroadcastReceiver 代码...
public class Notification_reciver extends BroadcastReceiver {
NotificationManager nm;
@SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent) {
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence title = " birthday...";
CharSequence message = " Today is your one Friends BirthDay";
Intent resultIntent = new Intent(context, MainScreenSlideActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Bundle b = intent.getExtras();
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, resultIntent, 0,b);
String userName=b.getString("userName");
Toast.makeText(context.getApplicationContext(), userName+" set in Notication reciver", Toast.LENGTH_LONG).show();
Notification notif = new Notification.Builder(context).setContentTitle("Today is your one Friends BirthDay")
.setContentText("Click to Birthday Wish").setSmallIcon(R.drawable.ic_launcher).build();
notif.setLatestEventInfo(context, title, message, contentIntent);
// hide the notification after its selected
notif.flags |= Notification.FLAG_AUTO_CANCEL;
nm.notify(0, notif);
}
以及我发送数据的 Activity 。
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getActivity(), Notification_reciver.class);
Bundle c = new Bundle();
c.putString("userName", "Sarbjot Singh");
Toast.makeText(context.getApplicationContext(), c + " ", Toast.LENGTH_LONG).show();
intent.setAction("" + Math.random());
intent.putExtras(c);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// PendingIntent pendingIntent =
// PendingIntent.getBroadcast(getActivity(), 0, intent, 0);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 00);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 00);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24 * 60 * 60 * 1000,
pendingIntent);
new Utils().setPreference("isFirstStart", false, getActivity().getApplicationContext());
最佳答案
我得到了答案 这是我的代码..;
AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, Notification_reciver.class);
Bundle c = new Bundle();
c.putString("userName", FirstName);
intent.setAction("" + Math.random());
intent.putExtras(c);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 00);
cal.set(Calendar.MINUTE, 00);
cal.set(Calendar.SECOND, 00);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 24 * 60 * 60 * 1000,
pendingIntent);
Utils.setPreference("isFirstStart", false,
FacebookBirthdaysActivity.this.getApplicationContext());
接收者。 @Override
public void onReceive(Context context, Intent intent) {
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent resultIntent = new Intent(context, SplashAnimationActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Bundle b = intent.getExtras();
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, resultIntent, 0, b);
String userName = b.getString("userName");
CharSequence title = " Birthday...";
CharSequence message = " Today is "+userName+" BirthDay";
Toast.makeText(context.getApplicationContext(), userName + " set in Notication reciver", Toast.LENGTH_LONG)
.show();
Notification notif = new Notification.Builder(context).setContentTitle("Today is your one Friends BirthDay")
.setContentText("Click to Birthday Wish").setSmallIcon(R.drawable.facebook).build();
// @SuppressWarnings("deprecation")
// Notification notif = new Notification(R.drawable.ic_launcher,
// "Today is your one Friends BirthDay", System.currentTimeMillis());
notif.setLatestEventInfo(context, title, message, contentIntent);
// hide the notification after its selected
notif.flags |= Notification.FLAG_AUTO_CANCEL;
/*
* Notification notif = new Notification(R.drawable.ic_launcher,
* "Crazy About Android...", System.currentTimeMillis());
* notif.setLatestEventInfo(context, from, message, contentIntent);
*/
nm.notify(0, notif);
}
关于android - 将额外数据从主要 Activity 传递到 BroadcastReceiver 时在 Bundle 中收到 Null,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24752380/