私はこのサイトを検索していて、アラームの設定に関連するいくつかの答えを見つけました。アラームを設定することができました。
私がしていることは:
- アクティビティから、特定の日時に受信者を呼び出すアラームを設定しました
- 受信者からサービスを呼び出します
- サービスから、(通知バーで)ユーザーに通知を送信します。
私の質問は次のとおりです。
今から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();
}