android notification の intent にセットした値が activity で受け取れない

| コメント(0) | トラックバック(0)

----
Service から notification を発行し、notification をタップした際に activityを起動、そのactivity に値を渡したかったので以下のようなコードを書いた。

Notification notification = new Notification(R.drawable.icon,"Some message",System.currentTimeMillis());
Intent intent = new Intent(this, com.example.SomeActivity.class) ;
intent.putExtra("PARAM", 1) ;
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,intent,0);
notification.setLatestEventInfo(this.getApplicationContext(),"Some message","Some explanation",pendingIntent);
notificationManager.notify(mAppNo, notification);

受け側の activity では

Intent intent = getIntent();
someValue = intent.getIntExtra("PARAM", 0) ;

みたいな感じで値を取得しようと思ったら、取得できない。
というか、いつも同じ値になってしまう。

調べてみたところ PendingIntent を作るところで PendingIntent.FLAG_UPDATE_CURRENT
というフラグを指定してあげないと、古いものが再利用されるらしい。

第四引数を足して
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
としたら、うまく受け渡しできるようになりました。

1時間ハマりました。



 

トラックバック(0)

トラックバックURL: http://onno.jp/mt/cgi-bin/mt-tb.cgi/123

コメントする

このブログ記事について

このページは、管理者が2011年11月 4日 19:19に書いたブログ記事です。

ひとつ前のブログ記事は「FLEX SDK ビルド時のエラー」です。

次のブログ記事は「Xperia arc で緊急地震速報の設定をする」です。

最近のコンテンツはインデックスページで見られます。過去に書かれたものはアーカイブのページで見られます。

Powered by Movable Type 5.13-ja