我一直在搜索这个网站,找到了一些与设置闹钟相关的答案。我已经成功地设置了闹钟。
我做的是:
- 我在某项 Activity 中设置了一个闹钟,它会在特定的时间和日期调用接收者
- 我从接收器调用服务
- 我通过服务向用户发送通知(在通知栏上)。
我的问题是:
我设置了 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 .
关于android - 如何设置即使我重启我的安卓手机也会响起的闹钟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3086057/