0

下面我有两个通知的代码,并且在意图我正在传递额外内容key1key2NotificationPillDescription.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)) );
                }
            }               
4

2 回答 2

0

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

这里:

在 startActivity() 上传递一个 Bundle?

这也可能有帮助:

putExtras 多个 putString 不起作用

于 2012-09-27T05:15:35.197 回答
0

它应该是,

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

代替

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

于 2012-09-27T05:09:14.883 回答