如何设置即使我重新启动 Android 手机也会响起的闹钟?

发布于 2024-09-06 22:08:42 字数 1591 浏览 8 评论 0原文

我一直在搜索这个网站并找到了一些与设置闹钟相关的答案。我已经成功设置闹钟了。

我所做的是:

  • 从活动中设置一个警报,该警报在特定时间和日期将调用接收器
  • 从接收器调用服务
  • 从服务发送通知(在通知栏上)给用户。

我的问题是:

  1. 我设置了 5 分钟后的闹钟。假设我关闭手机并重新打开(它似乎忘记了闹钟)。我怎样才能防止这种情况发生?

  2. 我真的需要调用服务来发送通知还是可以从接收器中执行此操作?

以下是上一节 (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:

  1. 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?

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你在我安 2024-09-13 22:08:42

您不需要调用服务来发送通知,您可以从接收器中执行此操作。

我认为没有办法在断电后保存警报。
我会做什么:

请注意,您需要将警报信息保存在某处。检查 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文