【问题标题】:Null received in Bundle while passing extra data from main activity to BroadcastReceiver在将额外数据从主要活动传递到 BroadcastReceiver 时在 Bundle 中收到 Null
【发布时间】:2014-07-15 07:31:52
【问题描述】:

当我从一个调用 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());

【问题讨论】:

    标签: android android-pendingintent android-broadcast


    【解决方案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());
    

    接收者是。

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

    【讨论】:

      【解决方案2】:

      在发送类中,使用:

      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. 
      

      【讨论】:

        【解决方案3】:

        看看这个Android Description,你需要:

        键必须包含包前缀,例如应用程序 com.android.contacts 将使用“com.android.contacts.ShowAll”之类的名称。

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

        【讨论】:

        • 但是那个键没有什么好玩的......它支持正常。 intent.putExtras(c);在接收方是它的 is.Bundle b = intent.getExtras();而真正的得到是.String userName = b.getString("userName");
        • @Sarbjot_Singh 您是否尝试将您的包名添加为密钥的前缀?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多