アラームに添付されたテキスト文字列に問題があります。アラームがトリガーされると、アラームに付けられた文字列が null になっているようです。明らかに私は間違いを犯しました - しかし、どこにあるのかわかりません。
アラームを設定するための私のコードは次のとおりです。
static void set_alarm(long alarm_time_in_millis,Context cont,AlarmManager alarm_manager,String str)
{
Intent intent = new Intent(cont, to_call_when_alarm_goes_off.class);
intent.putExtra("string_passed_in_bundle", str);
Log.i("xx","set_alarm ["+str+"]"); // The string I see in the Log is correct
PendingIntent pIntent = PendingIntent.getBroadcast(cont,0, intent, 0);
alarm_manager.cancel(pIntent);
alarm_manager.set(AlarmManager.RTC_WAKEUP,alarm_time_in_millis, pIntent);
}
アラームを受信するコードは次のとおりです。
public class to_call_when_alarm_goes_off extends BroadcastReceiver
{
Bundle bundle_from_whoever_called_this_activity;
@Override
public void onReceive(Context arg0, Intent arg1)
{
String str;
bundle_from_whoever_called_this_activity = arg1.getExtras();
str = bundle_from_whoever_called_this_activity.getString("string_passed_in_bundle");
Log.i("xx","to_call_when_alarm_goes_off: TIME TO WAKE UP!!! ["+str+"]");
try
{
Intent i = new Intent(arg0, dingaling.class);
i.putExtra("string_passed_in_bundle", str);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
arg0.startActivity(i);
}
catch (Exception e)
{
}
}
}
アラームを設定してトリガーされるのを待つと、ログファイルには次のように表示されます
set_alarm [Go to meeting]
to_call_when_alarm_goes_off: TIME TO WAKE UP!!! [null]
編集:「スーパー」があるかもしれません。呼び出すのを忘れた関数?
編集: getBaseContext()、getApplicationContext()、「this」など、さまざまな関数に渡すコンテキストの種類についてよく混乱します...どこかに間違った種類のコンテキストが含まれていると、この問題が発生する可能性がありますか?