0

AlarmManagerを使用して、定義済みの時間にタスクを実行するようにスケジュールを設定しようとしています。 AlarmManagerは私のアプリケーションから初期化されており、BroadcastイベントがonReceive()のメソッド内で受信されたときに実行されます。を '15分後'と呼ぶようにcontext.startService(webServiceIntent);を使用してサービスを呼び出します。 ;何らかの理由でそれが機能していません。それはすべてそれがそれを停止するPendingIntentを停止します。AlaramManagerが定義済みの時間の後にタスクを実行するようにスケジュールを設定する

//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); 
} 

答えて

0

問題#1:ミリ秒があなたのsetRepeating()通話中にオーバーティック場合SystemClock.elapsedRealtime()は、過去にあったかもしれない

は、以下の関連情報源です。将来の時刻を指定してください。

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

問題#3:多くのデバイスがstartService()コールとonHandleIntent()の間で眠ってしまったので、私はWakefulIntentServiceと書いています。

+0

ええと、私はそれを実際に実行して、AlarmManager.ELAPSED_REALTIME_WAKEUPをAlarmManager.RTCに置き換えました。しかし、今問題は、この行の中では、WakefulIntentService PendingIntent.getBroadcast(this、0、getLocationPollerIntent()、PendingIntent.FLAG_UPDATE_CURRENT)です。 – Waqas

+0

@Waqas: 'getLocationPollerIntent()'は別の 'Intent'を返しています。これは余分なものとは異なるものです。 – CommonsWare

+0

html可視的なアクティビティから呼び出されたとき、およびこの覚醒したインメンツサービスから呼び出されたときにgetApplicationContext()が同じ/異なる値を返すかどうか知っていますか?その間に、異なる値を返す場合は、この意図を両方のために同じようにする方法がありますか? – Waqas

関連する問題