2011-10-10 153 views
3

我已經創建了一個android服務。我啓動服務並使用startService(intent)和stopService(intent)來停止服務。如何停止android服務?

我的問題是,即使我停止服務;它會在沒有任何顯式調用startService的情況下再次啓動。

任何幫助表示讚賞

+1

請發表一些代碼示例。 – kaspermoerch

+0

http://stackoverflow.com/questions/2176375/android-service-wont-stop –

+0

其他與非常相似的話題似乎回答這一個問題:參考[this](http://stackoverflow.com/questions/2176375/ android-service-wont-stop)和[this](http://stackoverflow.com/questions/2265217/why-cant-i-stop-a-service-in-android) – Houcine

回答

3

沒有適當的代碼示例,這是困難的,但有機會,你有一個綁定服務,不綁定呢。

的情況下,

你沒有它的約束,「this.stopSelf()」從服務中是這樣的伎倆的方法之一:

public class BatchUploadGpsData extends Service { 
    @Override 
    public void onCreate() { 
     Log.e(TAG, "here i am, rockin like a hurricane. onCreate service"); 
     this.stopSelf(); 
    } 

這是測試代碼的實際代碼片斷適用於android 2.2及更高版本

+0

不會停止自我()停止服務一旦啓動?我們如何讓服務運行呢? –

+2

@IgorG。在這種情況下,它會在執行其唯一一行代碼後停止服務權限。想象在「this.StopSelf()」之前有成千上萬行代碼。 –