2010-08-31 77 views
1

我有一個Android應用程序與後臺運行服務。Android - 如何確保我的服務重新啓動?

當服務崩潰或被Android殺死時,我可以看到Android試圖重新啓動它。

然而,服務從未實際重新啓動,我可以看到Android計劃重新啓動,但實際上發生新的事情。

我的代碼如下:


@Override 
public void onCreate() { 
    String ns = Context.NOTIFICATION_SERVICE; 
    mNotificationManager = (NotificationManager) getSystemService(ns); 
    mTelephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); 
    audio_service = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 


} 

// This is the old onStart method that will be called on the pre-2.0 
// platform. On 2.0 or later we override onStartCommand() so this 
// method will not be called. 
@Override 
public void onStart(Intent intent, int startId) { 
    handleCommand(intent); 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    handleCommand(intent); 
    // We want this service to continue running until it is explicitly 
    // stopped, so return sticky. 
    return START_STICKY; 
} 

void handleCommand(Intent intent) { 

     running = true; 
     mTelephonyManager.listen(new IncomingListener(), PhoneStateListener.LISTEN_CALL_STATE); 

    } 

有什麼在我的代碼,以使服務缺失​​的重新啓動?

回答

2

您是否嘗試過使用startForeground()

+0

是的,我實現了它,它給了我一些非常奇怪的行爲,即使應用程序和服務不應該運行,它也會彈出通知。事情是我嘗試用一​​個空的onStart()方法和服務似乎重新啓動罰款。 – 2010-08-31 10:03:04

+0

好的Donal ... – DeRagan 2010-08-31 10:32:21

相關問題