PendingIntent.getService()
は、IntentService onHandleIntent()
を指定された時間前に実行します。PendingIntent.getService()アラーム予定時刻前にIntentService onHandleIntentを呼び出す
これはコードです。
class MakeAlarm {
public static void scheduleAlarm(Context context) {
AlarmManager manager = ....;
boolean enabled = // Some determination logic ;
//Intent to trigger
Intent intent = new Intent(context, ReminderService.class);
PendingIntent operation = PendingIntent
.getService(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
if (enabled) {
//Gather the time preference
Calendar startTime = // Calendar instance with time set
//Start at the preferred time
//If that time has passed today, set for tomorrow
if (Calendar.getInstance().after(startTime)) {
startTime.add(Calendar.DATE, 1);
}
Log.d(TAG, "Scheduling reminder alarm");
manager.setInexactRepeating(
AlarmManager.RTC,
startTime.getTimeInMillis(),
AlarmManager.INTERVAL_DAY,
operation
);
} else {
Log.d(TAG, "Disabling reminder alarm");
manager.cancel(operation);
}
}
}
これが呼び出されているIntentServiceさ:アラームスケジュールは完璧
public class ReminderService extends IntentService {
private static final String TAG = ReminderService.class.getSimpleName();
public ReminderService() {
super(TAG);
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d(TAG, "reminder event triggered");
//Present a notification to the user
NotificationManager manager =
(NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
Notification note = new NotificationCompat.Builder(this)
.setContentTitle(getString(R.string.notification_title))
...
.build();
manager.notify(NOTIFICATION_ID, note);
}
}
取り組んでいるが、私はアラームをスケジュールする際に瞬時に通知を取得しています。ログから、IntentService onHandleIntentが即座に起動されることが示されます。
あなたのstartTimeは過去のようですね。それが正しいことを確認してください –
私のstartTimeはいつも将来です。 – Ehbraheem
あなたの開始時刻がどういうことが分かりますか?いくつかの例を挙げてください。ロギング? –