如何设置即使我重新启动 Android 手机也会响起的闹钟?
我一直在搜索这个网站并找到了一些与设置闹钟相关的答案。我已经成功设置闹钟了。
我所做的是:
- 从活动中设置一个警报,该警报在特定时间和日期将调用接收器
- 从接收器调用服务
- 从服务发送通知(在通知栏上)给用户。
我的问题是:
我设置了 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();
}
I've been searching this site and found some answers related to setting an alarm. I've successfully been able to set up an alarm.
What I do is:
- From an activity I set an alarm that at certain time and date will call a receiver
- From the receiver I call a service
- From the service I send a notification (on the notification bar) to the user.
My questions are:
I set up an alarm 5 minutes from now. Say I turn off the phone and turn it back on (it seems it forgets the alarm). How can I prevent this from happening?
Do I really need to call a service to send the notifications or can I do it from the receiver?
The following is the code referenced on previous section (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);
This is the code referenced on previous section (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();
}
This the code referenced on previous section (c):
@Override
public void onCreate() {
super.onCreate();
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
showNotification();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要调用服务来发送通知,您可以从接收器中执行此操作。
我认为没有办法在断电后保存警报。
我会做什么:
请注意,您需要将警报信息保存在某处。检查 Android 的数据存储。
You don't need to call a service to send the notifications, you can do it from the receiver.
I don't think there is a way to save the alarm after a power off.
What I would do:
Notice that you would need to save the alarm info somewhere. Check Android's Data-storage.