在将主要活动中的额外数据传递给BroadcastReceiver时,在Bundle中收到Null

时间:2014-07-15 07:31:53

标签: android android-pendingintent android-broadcast

当我从一个活动发送数据时,它调用BroadcastReceiver,我在BroadcastReceiver中收到空。

这是我的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());

3 个答案:

答案 0 :(得分:1)

我得到了答案这是我的代码..;

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());

reciver是。

@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);
}

答案 1 :(得分:0)

请查看此Android Description,您需要:

  

密钥必须包含包前缀,例如app com.android.contacts将使用“com.android.contacts.ShowAll”这样的名称。

IMOP,使用intent.putExtra(key,value)并通过intent.getStringExtra(String key)进行检索要容易得多。

答案 2 :(得分:0)

在发送课程时,请使用:

myIntent.putExtra(key, String); // Where key is the name of string which you will use to catch in other class and String is your data string.

在接收课程中使用:

Intent myIntent = getIntent();
myIntent.getStringExtra(key);  //here key will the string that you set on sending class.