2013-05-06 54 views
0

我有一個服務,它由活動中的按鈕運行。該服務註冊一個廣播接收器。我知道當onStartCommand方法啓動時(bundle bundle = intent.getExtras()),從活動中獲取數據的方式;Android - 如何將數據發送到服務的onDestroy方法

我的問題:如何在onDestroy方法啓動時從活動中獲取數據?換句話說:我想關注當調用onDestroy方法時用戶選中的複選框,因爲這些操作取決於複選框。

任何想法?

感謝

+0

東西聽起來很奇怪。爲什麼服務關心複選框被檢查?服務如何具有複選框?該信息是否不應該以意向方式發送?當你說「行動」你的意思是什麼服務? – dmon 2013-05-06 02:50:39

+0

該服務可以註冊(使用按鈕)3個BroadcastReceivers(它取決於Activit中的3個複選框)。我在這個活動中也有一個「停止」按鈕。當我點擊右邊的複選框並點擊這個按鈕時,我停止了服務,然後我在onDestroy方法中。我希望這個方法在服務類中知道我的活動中的複選框是否被選中。這更清楚嗎?如您所說,我使用Intent將數據發送到onStartCommand方法。 stopservice方法在其聲明中不包含意圖。 – frontal 2013-05-06 03:08:36

回答

0

與其說stopService你可以調用startService並傳入一個布爾「關機」等於要傳遞真實和其它信息,在onStartCommand您可以檢查「關機」標誌並在信息中處理另一個通行證,然後調用stopSelf()。示例代碼

Intent intent = new Intent(this, MyService.class); 
intent.putExtra("shutdown", true); 
intent.putExtra(otherdata); 
startService(intent); 

在爲MyService onStartCommand

@Override 
public int onStartCommand(Intent intent, int flags, int startId) 
{ 
     if (intent != null) 
     { 
      // get the data and process 
      if (intent.getBooleanExtra("shutdown", false); 
      { 
       stopSelf(); 
      } 
      } 
    return START_STICKY; 
} 
0

您可以在sharedperference保存數據,並檢查它在你的服務ondestory。

相關問題