2017-01-09 26 views
2

这个问题问了这么多次,但我没有得到有关如何在onTaskRemoved再次启动 服务()服务onTaskRemoved()如何启动服务再次

我尝试以下方法,但它不会对任何工作解决方案我。

Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass()); 
    PendingIntent restartServicePendingIntent = PendingIntent.getService(
      getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT); 
    AlarmManager alarmService = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
    alarmService.set(alarmService.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 1000,restartServicePendingIntent); 
+0

为什么标志ONE_SHOT没有更多的想法? –

+0

@TimCastelijns我想要的是一次性使用挂起的意图 –

回答

0
 @Override 
    public void onTaskRemoved(Intent rootIntent) { 
     Log.e("FLAGX : ", ServiceInfo.FLAG_STOP_WITH_TASK + ""); 

     Intent restartServiceIntent = new Intent(getApplicationContext(), 
       this.getClass()); 
     restartServiceIntent.setPackage(getPackageName()); 

     Intent intent = new Intent(getApplicationContext(), this.getClass()); 

     intent.setPackage(getPackageName()); 

     PendingIntent restartServicePendingIntent = PendingIntent.getService(
       getApplicationContext(), 1, restartServiceIntent, 
       PendingIntent.FLAG_ONE_SHOT); 
     AlarmManager alarmService = (AlarmManager) getApplicationContext() 
       .getSystemService(Context.ALARM_SERVICE); 
     alarmService.set(AlarmManager.ELAPSED_REALTIME, 
       SystemClock.elapsedRealtime() + 1000, 
       restartServicePendingIntent); 

     super.onTaskRemoved(rootIntent); 
    } 

其实我它是如何工作的,但是这个代码帮助你..