私のアプリケーションでは、毎日特定のタスクを実行したいと思います。そのタスクが完了した後、複数の通知を別々のタイミングでトリガーしたいと思います。 グーグルでは、AlarmManagerを使用することが最善の選択だと分かりました。複数の通知を起動するようにAlarm Managerを設定する
このクラスは、システムアラームサービスへのアクセスを提供します。これにより、将来のある時点でアプリケーションを実行するようにスケジュールすることができます。アラームが解除されると、登録されたインテントがシステムによってブロードキャストされ、ターゲットアプリケーションがまだ実行されていない場合は自動的に開始されます。登録されたアラームは、デバイスがスリープしている間は保持されます(また、その時間中にデバイスがオフになった場合にデバイスを起動することもできます)が、オフになって再起動されるとクリアされます。
私の問題は次のとおりです。 1.通知は初めての場合のみ表示され、その後は表示されません。 2.そのアプリケーションを再起動するたびにAlarmManagerが起動され、過去の通知が表示されます。
PS:私はアンドロイドで初心者ですし、任意のヘルプは高く評価される。ここ
は、私が試したものです:
private void handleNotification() {
Intent alarmIntent = new Intent(this, AlarmReciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
:私はこのアラームを設定していますから、私の活動の
目的球
public class AlarmReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ArrayList<String> times = new ArrayList();
GPSTracker gpsTracker = new GPSTracker(context);
//calculate the times arraylist
DateFormat format;
Date date = null;
for (String s : times) {
if (hour12) {
format = new SimpleDateFormat("hh:mm a", Locale.ENGLISH);
try {
date = format.parse(s);
} catch (ParseException e) {
}
} else {
format = new SimpleDateFormat("hh:mm", Locale.ENGLISH);
try {
date = format.parse(s);
} catch (ParseException e) {
}
}
Intent alarmIntent = new Intent(context, NotificationReciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
Log.d(Constants.LOG_TAG, calendar.getTime().toString());
if (Build.VERSION.SDK_INT < 19) {
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
} else {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
}
}
}
:これは私のAlarmRecieverクラスです
そして、これが私のNotificationRecieverクラスです:
私のAndroidManifest.xmlでpublic class NotificationReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (Constants.D) Log.d(Constants.LOG_TAG, "NOTIFICATION RECIEVER");
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.sc_logo_final)
.setContentTitle(context.getResources().getString(R.string.app_name))
.setContentText(context.getResources().getString(R.string.app_name));
Intent resultIntent = new Intent(context, NavigationDrawerActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(NavigationDrawerActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
}
、
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".AlarmReciever">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver android:name=".NotificationReciever">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
残りの時間に通知が表示されない理由は何ですか? –
はい、私の答えを編集しました。 –
もう1つクリアしてください...私は位置の許可を取得した後にアラームを登録しています(オーバーライドされたメソッドは、アプリケーションが起動するたびに呼び出されます)...理想的にはいつアラームを登録すべきですか?私はこの方法をしようとした:http://stackoverflow.com/a/10836555/4451655それは助けていない.... –