0

AlarmManager事前定義された時間に実行するタスクをスケジュールするために使用しようとしています。これAlarmManagerはアプリケーションから初期化され、バックグラウンドで実行されます(これは正常に実行されます)。メソッドBroadcast内でイベントが受信されると、その内部をonReceive()使用してサービスを呼び出し、「15分後に」と言うようにcontext.startService(webServiceIntent);これを再スケジュールしようとしています。PendingIntentしかし、いくつかの理由でそれは機能していません。それはすべてそれを停止しPendingIntentます。

以下は関連する情報源です。

//Initiliazing AlarmManager from my app
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);             
pendingIntent = PendingIntent.getBroadcast(this, 0, getLocationPollerIntent(), 0);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(),  Constant.LOCATION_POLLING_PERIOD, pendingIntent);

//onReceive Method of BroadcastReceiver
@Override
public void onReceive(Context context, Intent intent) {
        Intent webServiceIntent = new Intent(context, LocationNotificationService.class);
        webServiceIntent.putExtra("UPDATED_LOCATION", loc);
    context.startService(webServiceIntent);
}

//Inside LocationNotificationService to reschedule the PendingIntent - This PendingIntent never get called

protected void onHandleIntent(Intent intent) {
       c.set(Calendar.HOUR_OF_DAY, 13);
       c.set(Calendar.MINUTE, 35);
       c.set(Calendar.SECOND, 0);

       AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
       PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, getLocationPollerIntent(), PendingIntent.FLAG_UPDATE_CURRENT);
       alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, c.getTimeInMillis(), Constant.LOCATION_POLLING_PERIOD, pendingIntent);
}
4

1 に答える 1

0

問題#1:通話SystemClock.elapsedRealtime()中にミリ秒が経過した場合は、過去に発生した可能性がありますsetRepeating()。必ず将来の時間を指定してください。

問題#2:デバイスの現在のタイムゾーンの13:35後でこのコードを実行している場合は、過去に発生した可能性があります。13:35必ず将来の時間を指定してください。

startService()問題#3:デバイスの多くがあなたの電話との間で眠りに落ちました。onHandleIntent()それが私が書いた理由ですWakefulIntentService

于 2012-05-06T12:26:00.893 に答える