13

AlarmManager と BroadcastReceiver クラス (AReceiver.java という名前) を使用して、いくつかの基本的なアラーム機能をプログラムに追加しています。私の問題は、PendingIntent を作成するインテントに添付されたバンドルに追加したデータが失われたように見えることです。AReceiver クラスでアクセスできる唯一のバンドル データは、android.intent.extra.ALARM_COUNT=1 です。

Intent、PendingIntent、および AlarmManager を作成するメイン アクティビティ クラスの基本コードは次のとおりです。 [メイン アクティビティのコード - Notepadv3]

Intent intent = new Intent(Notepadv3.this, AReceiver.class);         
intent.putExtra("teststring","hello, passed string in Extra");               
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, pendingPeriodIntentId, intent, 0);     
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);           
am.set(AlarmManager.RTC_WAKEUP, timeOfNextPeriod.getTimeInMillis(), alarmIntent);

[BroadcastReceiver のコード - AReceiver]

public void onReceive(Context con, Intent arg1) {
Bundle extrasBundle = arg1.getExtras();
Log.d("broadcast","contains teststring = " + extrasBundle.containsKey("teststring"));
Log.d("broadcast","is empty? = " + extrasBundle.isEmpty());
Log.d("broadcast","to string = " + extrasBundle.toString());
    }   

デバッグ メッセージは、テスト文字列が FALSE で、空が FALSE であり、バンドル全体を出力するときに android.intent.extra.ALARM_COUNT=1 値を取得することを示しています。

どんな助けでも大歓迎です。

乾杯、トム

4

1 に答える 1

35

この行を変更する必要があります

PendingIntent alarmIntent = PendingIntent.getBroadcast(this, pendingPeriodIntentId, intent, 0);

これに

PendingIntent alarmIntent = PendingIntent.getBroadcast(this, pendingPeriodIntentId, intent, PendingIntent.FLAG_UPDATE_CURRENT);

そうしないと、データが失われます

于 2010-09-17T14:41:25.350 に答える