android - 如何设置即使我重启我的安卓手机也会响起的闹钟?

标签 android alarm

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

我做的是:

  • 我在某项 Activity 中设置了一个闹钟,它会在特定的时间和日期调用接收者
  • 我从接收器调用服务
  • 我通过服务向用户发送通知(在通知栏上)。

我的问题是:

  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();
 }

最佳答案

您无需调用服务来发送通知,您可以从接收者那里完成。

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

请注意,您需要将警报信息保存在某处。检查 Android 的 Data-storage .

关于android - 如何设置即使我重启我的安卓手机也会响起的闹钟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3086057/

相关文章:

java - 将十六进制值 (0x45E213) 存储为整数

android - Android 应用程序需要哪些权限才能使用警报管理器服务?

android - 当服务调用 Activity 时唤醒手机并播放 MediaPlayer,Android

linux - alarm(2) - 当有人在等待时更改系统时间时它会做什么?

ListView 中的 Android 广告展示位置

android - 在android中使用JNI检索持久数据

android - 取消静音后,来电时手机只会振动,无法播放铃声

android - 如何为 iOS 和 Android 开发应用内更新?

xcode - Swift闹钟UIswitch

c - GDB SIGALRM,闹钟终止