我一直在搜索这个网站,找到了一些有关设置警报的答案。我成功地设置了警报。
我做的是:
在一个活动中,我设置了一个警报,在某个时间和日期将呼叫一个接收者
从接受者那里我呼叫服务
我从服务向用户发送通知(在通知栏上)。
我的问题是:
我在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();
}
最佳答案
您不需要调用服务来发送通知,您可以从接收者处执行此操作。
我认为断电后没有办法挽救警报。
我会做什么:
Start a service at boot
检查是否需要解决警报。
请注意,您需要将警报信息保存在某个地方。检查android的Data-storage。