当我从一个活动发送数据时,它会调用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);
}
我正在发送数据的活动是哪个。
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());