0

사용자가 recents 화면에서 앱을 지우더라도 서비스가 시작된 후에 백그라운드에서 계속 실행되도록하는 방법이 있습니까? 완벽한 예는 판도라의 앱으로, 재생이 시작되면 음악을 지우고 음악이 계속 재생되며 알림이 유지됩니다.앱이 삭제 된 경우에도 서비스를 계속 실행하는 방법은 무엇입니까?

내 샘플 앱에는 서비스가 실행 중인지 확인할 수 있도록 500ms의 현재 시간을 기록하는 간단한 핸들러가 있으며 하나의 토글로 시작하도록 4 개의 토글 중 하나를 사용하여 시작합니다 4 가지 서비스 시작 유형 (START_STICKY, START_NOT_STICKY, START_REDELIVER_INTENT, START_STICKY_COMPATIBILITY) 서비스를 시작하고 앱을 종료 한 다음 다시 읽은 내용을 지 웁니다. 각각의 경우에 서비스가 종료되고 루프 로깅이 중지됩니다.

MainActivity.java

public class MainActivity extends Activity { 

    private Boolean recording = false; 
    private Intent recordingService; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     recordingService = new Intent(this, RecordingService.class); 

     Button START_STICKY = (Button) findViewById(R.id.START_STICKY); 
     START_STICKY.setOnClickListener(new OnClickListener(){ 
      @Override 
      public void onClick(View arg0) { 
       startStopRecordingService(Service.START_STICKY); 
      } 
     }); 

     Button START_NOT_STICKY = (Button) findViewById(R.id.START_NOT_STICKY); 
     START_NOT_STICKY.setOnClickListener(new OnClickListener(){ 
      @Override 
      public void onClick(View arg0) { 
       startStopRecordingService(Service.START_NOT_STICKY); 
      } 
     }); 

     Button START_REDELIVER_INTENT = (Button) findViewById(R.id.START_REDELIVER_INTENT); 
     START_REDELIVER_INTENT.setOnClickListener(new OnClickListener(){ 
      @Override 
      public void onClick(View arg0) { 
       startStopRecordingService(Service.START_REDELIVER_INTENT); 
      } 
     }); 

     Button START_STICKY_COMPATIBILITY = (Button) findViewById(R.id.START_STICKY_COMPATIBILITY); 
     START_STICKY_COMPATIBILITY.setOnClickListener(new OnClickListener(){ 
      @Override 
      public void onClick(View arg0) { 
       startStopRecordingService(Service.START_STICKY_COMPATIBILITY); 
      } 
     }); 
    } 

    private void startStopRecordingService(int START_TYPE) { 
     if (recording) { 
      recordingService.removeExtra(RecordingService.START_TYPE); 
      stopService(recordingService); 
     } else { 
      recordingService.putExtra(RecordingService.START_TYPE, START_TYPE); 
      startService(recordingService); 
     } 

     recording = !recording; 
    } 
} 

RecordingService.java

public class RecordingService extends Service 
{ 
    public final static String START_TYPE = "START_TYPE"; 

    @Override 
    public IBinder onBind(Intent arg0) { 
     return null; 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startID) { 
     Log.i(getClass().getName(), "onStartCommand"); 

     int start_type = intent.getExtras().getInt(START_TYPE); 
     Log.i(getClass().getName(), "START_STICKY: " + String.valueOf(start_type == Service.START_STICKY)); 
     Log.i(getClass().getName(), "START_NOT_STICKY: " + String.valueOf(start_type == Service.START_NOT_STICKY)); 
     Log.i(getClass().getName(), "START_REDELIVER_INTENT: " + String.valueOf(start_type == Service.START_REDELIVER_INTENT)); 
     Log.i(getClass().getName(), "START_STICKY_COMPATIBILITY: " + String.valueOf(start_type == Service.START_STICKY_COMPATIBILITY)); 

     logTime(); 

     return start_type; 
    } 

    @Override 
    public void onDestroy() { 
     Log.i(getClass().getName(), "onDestroy"); 
     updateDisplayTimer.removeCallbacks(updateDisplayHandler); 
    } 

    public void logTime() { 
     Log.i(getClass().getName(), String.valueOf(System.currentTimeMillis())); 
     updateDisplayTimer.postDelayed(updateDisplayHandler, LOOP_TIME); 
    } 

    private static final int LOOP_TIME = 500; 
    private Handler updateDisplayTimer = new Handler(); 
    private Runnable updateDisplayHandler = new Runnable() { 
     @Override 
     public void run() { 
      logTime(); 
     } 
    }; 
} 

답변

2

전경 서비스를 구현하십시오. foreground service

전경 서비스는 알림을 표시하며 절대로 중지되지 않습니다.

하면 서비스는이 코드를 구현

Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text), 
     System.currentTimeMillis()); 
Intent notificationIntent = new Intent(this, ExampleActivity.class); 
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 
notification.setLatestEventInfo(this, getText(R.string.notification_title), 
     getText(R.string.notification_message), pendingIntent); 
startForeground(ONGOING_NOTIFICATION_ID, notification); 
관련 문제