0

IntentService을 작성하고 for looponHandleIntent 메소드에 넣으십시오. 매번 앱을 종료 할 때마다 (강제 종료가 아닌 최근 종료에서 제거) 중지되었습니다. 그러나 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

답변

1

사용 :
여기 내 샘플 코드

public class MyIntentService extends IntentService { 


    private static final String TAG = "MyIntentService"; 

    public MyIntentService() { 
     super("MyIntentService"); 
    } 


    @Override 
    protected void onHandleIntent(Intent intent) { 

     for (int i = 0; i < 30; i++) { 
      Log.d(TAG, "onHandleIntent: " + i); 
      try { 
       Thread.sleep(600); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 


    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     Log.d(TAG, "onDestroy: "); 
    } 
} 

심판이다.

+0

왜 내가 onTaskRemoved를 사용해야합니까? 메모리가 부족 해지면 서비스가 종료되지 않습니다. Android doc에 따르면이 기능은 기본 기능입니다. –

0
@Override 
    public void onTaskRemoved(Intent rootIntent) { 
} 

위의 방법은 앱이 최근에 삭제 될 때 호출됩니다. 그러나 아무 맥락도 없을 것입니다. 따라서 컨텍스트를 사용할 수있을 때 작업을 수행해야합니다. 그래서 내부의 장소 코드,

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 

    //do your operations 
    return START_REDELIVER_INTENT; 
} 

내부 기억 onStartCommand 당신은 START_REDELIVER_INTENT 또는 START_STICKY 중 하나를 반환해야합니다. 당신은 차이를 얻을 수 있습니다 from here.

그리고 또 하나, onStartCommand는 코드의 어느 곳에서나 startService가 한 번만 호출 될 때만 자동으로 취소됩니다.

그래서, 위의 코드를 다음 onStartCommand 서비스가 중지되지 않은 경우 주기적으로 취소됩니다

startService(new Intent(context, serviceName.class));

를 호출하여 서비스를 실행합니다.

+0

onTaskRemoved를 사용해야하는 이유는 무엇입니까? 메모리가 부족 해지면 서비스가 종료되지 않습니다. Android doc에 따르면이 기능은 기본 기능입니다. –

+0

여기에 대한 설명이 있습니다. - https://stackoverflow.com/questions/20392139/close-the-service-when-remove-the-app-via-swipe-in-android?rq=1 – Exigente05

관련 문제