포어 그라운드에서 실행되는 dummy IntentService
을 만들었습니다. 기본적으로 5 초마다 얼마나 오래 깨어 있었는지 기록합니다. 이것은 WakeLock
권한 없이도 테스트 장치에서 몇 시간 동안 실행되었습니다. 또한, 배터리 수명을 전혀 손상시키지 않는 것처럼 보입니다. 기기의 배터리 상태에 배터리의 1 %까지 사용하는 것으로 표시되지 않습니다. WakeLock
없이도이 서비스를 계속 실행할 수 있습니까?왜 IntentService에 WakeLock이 필요하지 않습니까?
업데이트 : 내가이 사실을 발견했습니다. 서비스가 실제로 잠을 자고있는 것 같지만, 상당히 모순이 있습니다. 일부 로그 문을 살펴보면 스레드가 5 초 동안 만 잠자기 상태가 되어도 깨어 난 후에도 시스템이 서비스를 일시 중지 한 것처럼 보일 수 있습니다. 시간은 17:56:31에서 17:56:54에서 17:57:05로 점프합니다. 23 초 점프와 9 초 점프. 이것이 왜 가장 도움이되는지에 대한 설명. 고맙습니다.
12-01 17:56:31.479 8723-8780/com.example.timedrift D/Done Sleeping﹕ Active for 2375000 Seconds
12-01 17:56:54.630 8723-8780/com.example.timedrift D/Done Sleeping﹕ Active for 2380000 Seconds
12-01 17:57:05.632 8723-8780/com.example.timedrift D/Done Sleeping﹕ Active for 2385000 Seconds
12-01 17:57:11.097 8723-8780/com.example.timedrift D/Done Sleeping﹕ Active for 2390000 Seconds
12-01 17:57:16.098 8723-8780/com.example.timedrift D/Done Sleeping﹕ Active for 2395000 Seconds
12-01 17:58:00.829 8723-8780/com.example.timedrift D/Done Sleeping﹕ Active for 2400000 Seconds
IntentService :
@Override
protected void onHandleIntent(Intent intent) {
Notification notification = new Notification(R.drawable.ic_launcher, "Time",
System.currentTimeMillis());
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, "Time",
"Time", pendingIntent);
startForeground(100, notification);
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPrefs.edit();
try {
Log.d("TimerService", "Start");
int i = 1;
while(true){
Thread.sleep(5000);
int numberOfSeconds = i++*5;
Log.d("Done Sleeping", String.valueOf("Active for "+numberOfSeconds+" Seconds"));
editor.putLong(String.valueOf(numberOfSeconds), numberOfSeconds);
editor.apply();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
일부 관련 설명 http://android.stackexchange.com/questions/69693/how-do-timers-and-alarms-function-while-the-device-is-sleeping – zapl