2017-05-16 59 views
2

這是我的服務:當我強制關閉我的應用時服務停止。我怎樣才能使服務保持?

public class KeepAliveService extends Service { 
Alarm alarm = new Alarm(); 
public void onCreate() 
{ 
    Log.i("","KEEPALIVE onCreate"); 
    super.onCreate(); 
    Notification notification = new Notification(); 
    startForeground(1337, notification); 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) 
{ 
    alarm.setAlarm(this); 
    Log.i("","KEEPALIVE onCreate start command"); 
    return START_STICKY; 
} 

@Override 
public void onStart(Intent intent, int startId) 
{ 
    alarm.setAlarm(this); 
    Log.i("","KEEPALIVE onStart"); 
} 

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

我在清單中有這樣的:

<service 
     android:name="com.vidyo.vidyomod.KeepAliveService" 
     android:exported="true" 
     android:process=":KeepAliveService" 
     android:enabled="true"> 
    </service> 
    <receiver android:process=":remote" android:name="com.vidyo.vidyomod.utils.Alarm"></receiver> 
    <receiver android:name="com.vidyo.vidyomod.utils.AutoStart"> 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED"></action> 
     </intent-filter> 
    </receiver> 

我象這樣開始:

Intent i= new Intent(BaseActivity.this, KeepAliveService.class); 
    startService(i); 

但是,當我清楚我的應用程序,從應用程序列表(電話廣場),我得到這個日誌,然後服務不再工作了:

05-16 14:52:22.698: I/ActivityManager(1102): Killing 9049:com.vidyo.vidyomod:KeepAliveService/u0a132, isShadow:false (adj 2): stop com.vidyo.vidyomod 
05-16 14:52:22.725: D/ActivityManager(1102): SVC-handleAppDiedLocked: app = ProcessRecord{ff18702 9049:com.vidyo.vidyomod:KeepAliveService/u0a132, isShadow:false}, app.pid = 9049 
05-16 14:52:22.726: W/ActivityManager(1102): Scheduling restart of crashed service com.vidyo.vidyomod/.KeepAliveService in 1000ms 
05-16 14:52:22.726: V/BroadcastQueue(1102): removeReceiverListLocked app = ProcessRecord{ff18702 9049:com.vidyo.vidyomod:KeepAliveService/u0a132, isShadow:false} 
05-16 14:52:22.726: V/BroadcastQueue(1102): removeReceiverListLocked app = ProcessRecord{ff18702 9049:com.vidyo.vidyomod:KeepAliveService/u0a132, isShadow:false} 
05-16 14:52:22.726: V/BroadcastQueue(1102): removeReceiverListLocked app = ProcessRecord{ff18702 9049:com.vidyo.vidyomod:KeepAliveService/u0a132, isShadow:false} 
05-16 14:52:22.857: I/ActivityManager(1102): Force stopping service ServiceRecord{3c2a0a1 u0 com.vidyo.vidyomod/.KeepAliveService, isShadow:false} 
05-16 14:52:22.859: W/ActivityManager(1102): Spurious death for ProcessRecord{ff18702 0:com.vidyo.vidyomod:KeepAliveService/u0a132, isShadow:false}, curProc for 9049: null 

編輯:

現在在我的活動我這樣做:

Intent i= new Intent(VidyoModApplication.this, KeepAliveService.class); 
    startService(i); 

而在我的服務,在onStardComand我這樣做:

@Override 
public int onStartCommand(Intent intent, int flags, int startId) 
{ 
    alarm.setAlarm(this); 
    Log.i("","KEEPALIVE onCreate start command"); 
    Intent notificationIntent = new Intent(Intent.ACTION_MAIN); 
    notificationIntent.addCategory(Intent.CATEGORY_HOME); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 

    Notification notification = new Notification.Builder(this) 
      .setContentTitle(getText(R.string.app_name)) 
      .setContentText("Service is running") 
      .setSmallIcon(R.drawable.ic_launcher) 
      .setContentIntent(pendingIntent) 
      .build(); 

    startForeground(1337, notification); 
    return START_STICKY; 
} 

但儘管如此,當我強迫關閉應用程序,它停止服務。 找到這個例子後試過了: http://www.truiton.com/2014/10/android-foreground-service-example/

+0

哪個版本的Android,你在測試此? –

+0

4.2.2,5和6 –

+0

您正在測試哪些設備? –

回答

3

我打算在這邊出去。我不知道您使用的具體設備。但是,有許多設備爲「受保護」或「特殊」應用提供特殊設置頁面,允許其在後臺運行。如果您有這些設備之一,並且您的應用不在「受保護」應用列表中,並且您的Service已被殺害,則Android不會重新啓動它,即使您已將其宣佈爲前景Service並從onStartCommand()返回START_STICKY

請檢查您是否遇到此問題的設備上是否有這種情況。

https://stackoverflow.com/a/41369032/769265https://stackoverflow.com/a/42120277/769265

+0

非常感謝,我將分析這 –

+1

這就是它。我檢查了您的答案是正確的,因爲我的應用程序不在受保護的應用程序列表中 –

相關問題