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()는, 과거에되었을 수도 있습니다

다음은 관련 소스입니다. 나중에 시간을 지정하십시오.

13:35 이후에이 코드를 실행하는 경우 문제 2 번 : 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

흠 보이는 활동 내에서 getApplicationContext()가 동일한/다른 값을 반환하는지,이 깨어있는 인 텐트 서비스에서 호출되었을 때 알 수 있습니까? 사이에 다른 값을 반환한다면이 아이디어를 둘 다 어떻게 동일하게 만들 수 있을지 생각해보십시오. – Waqas

관련 문제