我一直在搜索这个网站,并找到了一些与设置警报相关的答案。我已经成功设置了闹钟。
我要做的是:
- 从一项活动中,我设置了一个警报,在特定时间和日期将呼叫接收器
- 从接收器我打电话给服务
- 从服务中,我向用户发送通知(在通知栏上)。
我的问题是:
我在 5 分钟后设置了闹钟。假设我关闭手机并重新打开(它似乎忘记了闹钟)。我怎样才能防止这种情况发生?
我真的需要调用服务来发送通知还是可以从接收者那里完成?
以下是上一节 (a) 中引用的代码:
Intent intent = new Intent(MyActivity.this,
AlarmReceiver.class);
intent.putExtra("alarm_message", "Something");
PendingIntent mAlarmSender;
mAlarmSender = PendingIntent.getBroadcast(
MyActivity.this, 0, intent, 0);
// We want the alarm to go off 30 seconds from now.
long alarmTime = dateMgmt.getTimeForAlarm(pickedDate);
// Schedule the alarm!
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, alarmTime + 15000,
mAlarmSender);
这是上一节 (b) 中引用的代码:
@Override
public void onReceive(Context context, Intent intent) {
try {
Bundle bundle = intent.getExtras();
String message = bundle.getString("alarm_message");
Intent newIntent = new Intent(context, MyService.class);
context.startService(newIntent);
} catch (Exception e) {
Toast
.makeText(
context,
"There was an error somewhere, but we still received an alarm",
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
这是上一节 (c) 中引用的代码:
@Override
public void onCreate() {
super.onCreate();
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
showNotification();
}