4

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

我要做的是:

  • 从一项活动中,我设置了一个警报,在特定时间和日期将呼叫接收器
  • 从接收器我打电话给服务
  • 从服务中,我向用户发送通知(在通知栏上)。

我的问题是:

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

1 回答 1

2

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

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

请注意,您需要将警报信息保存在某处。检查 Android 的数据存储

于 2010-06-21T15:58:23.283 回答