我对附加到警报的文本字符串有疑问。似乎触发警报时,附加到警报的字符串为空。显然我犯了一个错误-但不知道在哪里。
我设置闹钟的代码是;
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" 等...如果我在某处涉及错误类型的上下文,会导致这个问题吗?