0
アラームを作成して、2分ごとにアプリケーションを起動するよう通知することができます。すべて正常に動作しますが、アプリケーションを手動で起動すると通知が表示されます。ここに私のコードです:AlarmReceiverのAndroid通知
受信機:
public void onReceive(Context context, Intent intent) {
nm = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "myactivity";
CharSequence message = "click to start activity";
Intent scheduledIntent = new Intent(context, AmslerTestActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
scheduledIntent, 0);
Notification notif = new Notification(R.drawable.icon,
message, System.currentTimeMillis());
notif.setLatestEventInfo(context, from, message, contentIntent);
notif.flags = Notification.FLAG_AUTO_CANCEL;
notif.defaults |= Notification.DEFAULT_SOUND;
nm.notify(1, notif);
scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(scheduledIntent);
}
スケジューラ:
cal.set(Calendar.HOUR_OF_DAY, 12);
cal.set(Calendar.MINUTE, 01);
cal.set(Calendar.SECOND, 0);
Intent intent = new Intent(myactivity.this, AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
// Get the AlarmManager service
am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 2 * 60 * 1000, sender);
私は、コードが実行されていないだけでリングへの通知をしたいです。
あなたのお返事ありがとうございます。しかしそれは働かなかった。私はonDestroyに「スケジューラー」を配置し、通知はクリックして戻ってきました。 2分は "テスト"のことです。実際のものはINTERVAL_DAYとします。私は毎日12時01分に通知を出したい。放送受信機がすぐに受信されるようなものです。私が間違ってやったことは何ですか? – noviceAndroidprogrammer
これで今のようにアラームを設定していますよね?そうですね、am.setRepeating(AlarmManager.RTC_WAKEUP、System.currentTimeMillis()+(2 * 60 * 1000)、2 * 60 * 1000、sender);過去にトリガ時間を設定してアラームを設定すると、直ちにアラームが解除されます。また、あなたが設定したアラームがまだ残っているかもしれません。キャンセルすることもできますし、デバイス/エミュレータの電源を落として電源を入れなおして、アラームをクリアすることもできます – koopaking3
また、「クリックした分」とはどういう意味ですか? 'onDestroy'でアラームを設定した場合、' Activity'で 'finish()'を呼び出すか、システムがスペースを節約するためにアクティビティを破棄するときにのみ、そのコードが呼び出されます。それで、あなたが戻るボタンを押すと直ちにアラームをオフにすることはありそうにないようです。他の何かがここで起こっているに違いありません。 – koopaking3