2011-09-20 96 views
1

我有一個主要的類有兩個按鈕來啓動和停止服務。Android服務並沒有停止

public void onClick(View src) { 
    switch (src.getId()) { 
    case R.id.buttonStart: 
     Log.d(TAG, "onClick: starting srvice"); 
     myService = new Intent(this, MyService.class); 
     startService(myService); 
     break; 
    case R.id.buttonStop: 
     Log.d(TAG, "onClick: stopping srvice"); 
     stopService(myService); 
     break; 
    } 
} 

開始服務旨在將當前GPS座標發送到服務器。它開始完美,但並沒有停止按鈕點擊。

在MYService.java我有這個代碼onDestroy方法。

public void onDestroy() { 
    Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show(); 
    Log.d(TAG, "onDestroy"); 
} 

請幫助如何殺死此服務。

+0

檢查「的onClick:停止srvice」在日誌中,當您單擊該按鈕出現。它可能是一些愚蠢的東西,比如按鈕id中的拼寫錯誤,並且'stopService()'永遠不會被調用。 –

回答

0

試試這個停止服務stopService(itnt_BackServices);在這個例子中itnt_BackServices是服務的名稱。你不能在服務類中停止服務,而是停止任何活動。

0

嘗試添加此爲您服務

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    this.startId = startId; 
    return mStartMode; 
} 

@Override 
public void onDestroy() { 
    stopSelf(startId); 
} 
+0

我的onStop永遠不會停止我的後臺服務,嘗試過很多解決方案,但沒有運氣,請提出任何想法。 – Madhu