无法从使用通知的待定​​意图调用的意图中获取额外内容?

时间:2012-09-27 04:59:31

标签: android android-intent notifications

下面我有两个通知的代码,并且在意图中我通过key1key2传递额外内容,但在NotificationPillDescription.class我只能使用getExtras获取key1。

//第一次通知的通知代码

Notification notification = new Notification(R.drawable.ic_launcher, "Take your morning pill!", System.currentTimeMillis());
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notifyIntent = new Intent(getApplicationContext(), NotificationPillDescription.class);
    Bundle extras = new Bundle();
    extras.putString("Key1", String.valueOf(1));
    notifyIntent.putExtras(extras);

    contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notifyIntent, 0);
    notification.setLatestEventInfo(this, "Take your Morning pill!", "My Drug", contentIntent);
                        notificationManager.notify(dataProvider.notificationId++, notification);

//第二次通知的通知代码

notifyIntent = new Intent(getApplicationContext(), NotificationPillDescription.class);
        Bundle extras = new Bundle();
        extras.putString("Key2", String.valueOf(2));
        notifyIntent.putExtras(extras);

        contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notifyIntent, 0);
        notification.setLatestEventInfo(this, "Take your Morning pill!", "My Drug", contentIntent);
                            notificationManager.notify(dataProvider.notificationId++, notification);

// NotificationPillDescription.class的代码

Bundle extras = getIntent().getExtras();
            if(extras != null){
                if(extras.containsKey(String.valueOf(1)))
                {
                   Log.d("Pill Id0",extras.getString(String.valueOf(1)) );

                }
                else
                {
                    Log.d("Pill Id1",extras.getString(String.valueOf(2)) );
                }
            }               

2 个答案:

答案 0 :(得分:0)

应该是,

extras.getString("Key1")extras.getString("Key2")

而不是

extras.getString(String.valueOf(1))&& extras.getString(String.valueOf(2))

答案 1 :(得分:0)

看看这里。它描述了如何传递捆绑包。我认为你的extras.getstring可能还不够。如果你正在使用putextras,你可能也不需要声明一个包。

下面:

Passing a Bundle on startActivity()?

这也可能有用:

putExtras multiple putString not working