通知:传递数据

时间:2010-10-30 23:45:35

标签: android notifications

我正在尝试将数据从通知传递到活动:

btnShedule.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

            Intent notificationIntent = new Intent(Oconf.this, Oconf.class);
            notificationIntent.putExtra("test", 122);

            PendingIntent contentIntent = PendingIntent.getActivity(Oconf.this, 0, notificationIntent, 0);

            Notification notification = new Notification(R.drawable.notify_deal, "text", System.currentTimeMillis());
            notification.setLatestEventInfo(Oconf.this, getString(R.string.notify_events), "text", contentIntent);

            notification.defaults |= Notification.DEFAULT_SOUND;
            notification.flags |= Notification.FLAG_AUTO_CANCEL;

            notificationManager.notify(0, notification);
        }
    });

然后,onResume:

public void onResume() {

    String q = getIntent().getStringExtra("test");
    if (q == null) {
        Log.e(TAG, "null!");
    }
    else {
        Log.e(TAG, q);
    }

    super.onResume();
}

我得到了“null!”。哪里出错?

2 个答案:

答案 0 :(得分:3)

在我的情况下,当我点击通知时,onNewIntent()从未被调用过。但是我在onCreate()方法中添加了以下代码:

String m = getIntent().getStringExtra("put_extra_string");

我收到了我试图传递给通知意图的字符串。

答案 1 :(得分:2)

:)

protected void onNewIntent(Intent intent)