誤ったエキストラは、すべての意図を受け取ったが、最初の1
-
19-09-2019 - |
質問
私は、将来のイベントのリマインダーを設定するために使用することができる小さなアプリケーションを持っています。アプリは、ユーザーが思い出されるべきときのための時間を設定するAlarmManagerを使用しています。アラームがオフになると、BroadcastReceiverはこれを登録し、順番にトーストと、ステータスバーに通知を介してユーザに通知するサービスを開始します。
の通知とトーストで正しい情報を表示するために、いくつかの追加情報が意図とともに渡されます。リマインダーが登録されている最初の時間は、サービスに上BroadcastReceiverで受信され、渡された情報が正確です。しかし、それぞれ以下のリマインダー(BroadcastReceiverで受信された、すなわちそれぞれの新しい目的)のためにこの情報が送信される情報が異なる場合であっても同じままです。
文字列「fooが」最初の意図で余分として置かれている場合、例として、「foo」という正しくBroadcastReceiverによって抽出されます。 「バーは」第二の意図に余分として置かれている場合は、「foo」というまだBroadcastReceiverによって抽出されます。
これは、アラームを登録し、インテント(メインUIクラス)を通過するコードである
Intent intent = new Intent(ACTION_SET_ALARM);
intent.putExtra("desc", desc);
intent.putExtra("time", time);
intent.putExtra("dbId", dbId);
intent.putExtra("millis", millis);
PendingIntent pIntent = PendingIntent.getBroadcast(quickAlert.this, 0, intent, 0);
// Schedule the alarm!
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, millis, pIntent);
onReceive() - BroadcastReceiverクラスのメソッド
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, AlertService.class);
String desc = intent.getStringExtra("desc").equals("") ? "": ": " + intent.getStringExtra("desc");
String time = intent.getStringExtra("time");
long dbId = intent.getLongExtra("dbId", -1);
long millis = intent.getLongExtra("millis", -1);
i.putExtra("desc", desc);
i.putExtra("time", time);
i.putExtra("dbId", dbId);
i.putExtra("millis", millis);
Log.d(TAG, "AlertReceiver: " + desc + ", " + time + ", " + dbId + ", " + millis);
Toast.makeText(context, "Reminder: " + desc, Toast.LENGTH_LONG).show();
context.startService(i);
}
マニフェストに意図フィルタ
<receiver android:name=".AlertReceiver">
<intent-filter>
<action android:name="com.aspartame.quickAlert.ACTION_SET_ALARM" />
</intent-filter>
</receiver>
私は今、いくつかの時間のためにこれで立ち往生してきたのでヘルプは非常に高く評価される!
解決
あなたがそれを作成するときに、あなたのFLAG_UPDATE_CURRENT
に PendingIntent
に追加してみてくださいgetBroadcast()
を経由してます。
他のヒント
上記の答えは正しいですが、説明が不足しています。それは PendingIntentドキュメントするます:
PendingIntent自体は、単にそれを取得するために使用された元のデータを記述するシステムによって維持トークンへの参照です。 ...作成アプリケーションが後でPendingIntentの同じ種類(同じ操作、同じインテントアクション、データ、カテゴリ、コンポーネント、同じフラグ)を再取得した場合は、同じトークン
を表すPendingIntentを受信します
"余分な" データは、具体的に にPendingIntent-アイデンティティの概念に含まれていないこと!
注意最高のリンク:<のhref =「http://alvinalexander.com/source-code/android/android-how-attach-extra-intent-pendingintent-notification-solution-example」 rel = "nofollowを"> http://alvinalexander.com/source-code/android/android-how-attach-extra-intent-pendingintent-notification-solution-example の
これが手がかりです。
int uniqueInt = (int) (System.currentTimeMillis() & 0xfffffff);
PendingIntent.getService(context, uniqueInt, intent, 0)