从主活动传递额外数据到广播接收器时,在Bundle中收到了null

3

当我从一个活动发送数据时,它会调用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());
3个回答

2
我找到了答案,这是我的代码: ...
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);
}

1
在发送类中,使用:


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.

在Receiving类中使用:

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

0

看看这个Android描述,你需要:

键必须包括一个包前缀,例如应用程序com.android.contacts将使用类似于“com.android.contacts.ShowAll”的名称。

在我看来,更容易的方法是只需使用intent.putExtra(key,value)并通过intent.getStringExtra(String key)检索。


但是那个键没有任何乐趣...它支持正常使用。intent.putExtras(c);在接收端,它是.Bundle b = intent.getExtras();并且获取该变量的是.String userName = b.getString("userName"); - user2873376
@Sarbjot_Singh 你尝试将你的包名作为键的前缀添加了吗? - Kingfisher Phuoc

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接