IntentService
을 작성하고 for loop
을 onHandleIntent
메소드에 넣으십시오. 매번 앱을 종료 할 때마다 (강제 종료가 아닌 최근 종료에서 제거) 중지되었습니다. 그러나 onDestroy
이 (가) 호출되지 않았습니다. 다른 기기에서도 사용해 보았습니다. 나는 그것이 낮은 기억의 문제라고 생각하지 않는다.
서비스는 앱이 포 그라운드에있는 경우에만 사용된다는 의미입니까?
메인 스레드에서 backgound 작업을해야하고 사용자가 앱을 닫으면 서비스가 종료되었습니다. 100 밀리 초 후에 서비스를 다시 시작 onTaskRemoved 방법 위의 가까운 응용 프로그램Android 서비스가 앱 종료 직후에 중지됩니다.
public class MyService extends Service {
@Override
public int onStartCommand(final Intent intent, final int flags,
final int startId) {
super.onStartCommand(intent, flags, startId);
return Service.START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onTaskRemoved(Intent rootIntent) {
Intent restartService = new Intent(getApplicationContext(),
this.getClass());
restartService.setPackage(getPackageName());
PendingIntent restartServicePI = PendingIntent.getService(
getApplicationContext(), 1, restartService,
PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 100, restartServicePI);
Toast.makeText(this, "onTaskRemoved", Toast.LENGTH_SHORT).show();
super.onTaskRemoved(rootIntent);
}}
후 재시작 서비스에 대한 코드 아래 How to keep an IntentService running even when app is closed?
Service restarted on Application Close - START_STICKY
왜 내가 onTaskRemoved를 사용해야합니까? 메모리가 부족 해지면 서비스가 종료되지 않습니다. Android doc에 따르면이 기능은 기본 기능입니다. –