2016-07-27 65 views
1

我這是網上下載一個文件在前臺前臺服務是當應用程序被殺害得到立即的Android 4.1〜5.1.1打死

@Override 
    public int onStartCommand(Intent intent, int flags, int sid){ 
     this.sid = sid; 
     PendingIntent mainIntent = PendingIntent.getActivity(this, 0, MyApplication.mainIntent, 0); 
     Notification notification = new NotificationCompat.Builder(VuclipPrime.getInstance()) 
       .setSmallIcon(R.drawable.ic_launcher) 
       .setContentTitle("Downloading Video") 
       .setContentText("Initiating...") 
       .setContentIntent(mainIntent) 
       .setAutoCancel(false) 
       .setOngoing(true) 
       .build(); 
     startForeground(mNotificationId, notification); 
     if (VideoDownloadManager.currentVideoDownloadTask != null) { 
      VideoDownloadManager.currentVideoDownloadTask.start(); 
     } else { 
      stopSelf(); 
     } 
     return START_NOT_STICKY; 
    } 

當我刷卡殺應用程序服務,該服務被也停下來。 我嘗試了所有StackOverflow上的現有解決方案,但都沒有工作。 我嘗試了YouTube應用程序,但並未終止其服務。我如何實現這一目標?謝謝。

+0

21以上使用作業Sceduler api –

回答

0

解決方案是在服務的onTaskRemoved()方法中啓動一個虛擬活動,以防止服務被殺死。 也啓動後立即完成DummyActivity。

檢查下面的代碼: -

package com.your.application.services; 

import android.app.Notification; 
import android.app.PendingIntent; 
import android.app.Service; 
import android.content.Intent; 
import android.os.Build; 
import android.os.IBinder; 
import android.support.v4.app.NotificationCompat; 


public class DownloadService extends Service { 
    int mNotificationId = 001; 
    Notification notification; 
    private int sid; 

    public DownloadService() { 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int sid) { 
     this.sid = sid; 
     PendingIntent mainIntent = PendingIntent.getActivity(this, 0, YourApplication.getInstance().downloadService.mainIntent, 0); 
     notification = new NotificationCompat.Builder(YourApplication.getInstance()) 
       .setSmallIcon(R.drawable.ic_launcher) 
       .setContentTitle("Starting Service") 
       .setContentText("Initiating...") 
       .setContentIntent(mainIntent) 
       .setAutoCancel(true) 
       .setOngoing(true) 
       .build(); 
     startForeground(mNotificationId, notification); 
     if (YourApplication.getInstance().downloadService.videoDownloader != null) { 
      YourApplication.getInstance().downloadService.startDonwloadThread(YourApplication.getInstance().downloadService.videoDownloader); 
     } else { 
      stopSelf(); 
     } 
     return START_NOT_STICKY; 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
    } 

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

    @Override 
    public void onTaskRemoved(Intent rootIntent) { 
     if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1 && Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2) { 
      Intent intent = new Intent(this, DummyActivity.class); 
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      startActivity(intent); 
     } 
    } 
} 

的DummyActivity: -

public class DummyActivity extends Activity { 
    @Override 
    public void onCreate(Bundle bundle) { 
     super.onCreate(bundle); 
     finish(); 
    } 
} 

的AndroidManifest條目DummyActivity: -

  <activity 
      android:name=".downloader.DummyActivity" 
      android:allowTaskReparenting="true" 
      android:alwaysRetainTaskState="false" 
      android:clearTaskOnLaunch="true" 
      android:enabled="true" 
      android:excludeFromRecents="true" 
      android:finishOnTaskLaunch="true" 
      android:noHistory="true" 
      android:stateNotNeeded="true" 
      android:theme="@android:style/Theme.NoDisplay" /> 
+0

你是什麼意思那? –

+0

@comoran我做了一個前臺服務,並沒有死亡。我不知道,你爲什麼這樣做。但是,感謝分享知識!做得好!! –

0

你是用NOT_STICKY參數的startinf服務。 您應該返回START_STICKY內onStartCommand()

@Override 
    public int onStartCommand(Intent intent, int flags, int sid){ 
     this.sid = sid; 
     PendingIntent mainIntent = PendingIntent.getActivity(this, 0, VuclipPrime.mainIntent, 0); 
     Notification notification = new NotificationCompat.Builder(VuclipPrime.getInstance()) 
       .setSmallIcon(R.drawable.ic_launcher) 
       .setContentTitle("Downloading Video") 
       .setContentText("Initiating...") 
       .setContentIntent(mainIntent) 
       .setAutoCancel(false) 
       .setOngoing(true) 
       .build(); 
     startForeground(mNotificationId, notification); 
     if (VideoDownloadManager.currentVideoDownloadTask != null) { 
      VideoDownloadManager.currentVideoDownloadTask.start(); 
     } else { 
      stopSelf(); 
     } 
     return START_STICKY; 
    } 

從技術文檔:

https://developer.android.com/reference/android/app/Service.html#START_STICKY

  • START_STICKY:系統會嘗試再次啓動該服務時 它殺害。
  • START_NO_STICKY:系統不會關心 服務是否被終止。
+0

我試過了,仍然服務正在死亡 –

+0

你也可能想在[自己的過程]中運行服務(https://developer.android.com/guide/topics/清單/服務element.html#PROC)。 – GPuschka

+0

當服務停止或銷燬時,您是否打電話給stopSelf()?甚至在您的onStartCommand的「if」中。也許這可能是問題所在。 – adalPaRi

0

的Android中止進程,如果它不是上市當進程處於睡眠模式時受保護的應用。轉到設置 - >保護的應用程序 - >選擇您的應用程序並啓用作爲受保護的應用程序。

相關問題