2014-10-31 48 views
8

我在主活動中放置了一個開關小部件,我還有第二個活動來擴展BroadcastReceiver。我想在第二個活動中獲取開關小部件的布爾狀態。如何在Android中獲取開關值?

如果鍵入

Switch s = (Switch) findViewById(R.id.switch1); 

它說findViewById是不確定的類型SecondActivity。問題是Android不允許我在擴展Broadcast Receiver的類中獲得交換機的價值。

我想知道開關的狀態,即開關是打開還是關閉,但是在第二個活動中。我怎樣才能實現它?

+0

可能重複:http://stackoverflow.com/a/10577852/2777098 – 2014-10-31 14:00:08

+0

@IsabelHM我已經告訴Android的沒有按」 t在擴展廣播接收器的類中識別findViewById。我已經讀過這個問題,完全不同。 – 2014-10-31 14:01:58

+2

您的BroadcastReceiver沒有UI(因此沒有開關),所以它不會讓你找到ViewById()。 – 2014-10-31 14:06:38

回答

10

從Activity調用findViewById()只能訪問作爲該Activity佈局一部分的Views。您不能使用它來搜索任何其他活動的佈局。

根據您的應用程序的設計,您可以使用其中的一個:

1)通過一個Intent額外

發送切換到SecondActivity的值。在活動:

Intent mIntent = new Intent(this, SecondActivity.class); 
mIntent.putExtra("switch", s.isChecked()); 
startActivity(mIntent); 

In SecondActivity:

boolean isChecked = getIntent().getBooleanExtra("switch", false); 

2)的值保存到上變化的偏好,和在SecondActivity

讀偏好在活動:

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); 
SharedPreferences.Editor e = settings.edit(); 
e.putBoolean("switch", s.isChecked()); 
e.commit(); 

在SecondActivity:

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); 
boolean isChecked = settings.getBoolean("switch", false); 
+0

如何將布爾值添加到首選項並在SecondActivity中檢索。你可以請郵編嗎? – 2014-10-31 14:13:15

+0

我在SecondActivity中收到錯誤。這是錯誤: - 「類型PreferenceManager中的方法getDefaultSharedPreferences(Context)不適用於參數(SecondActivity)」。 – 2014-10-31 14:16:41

+0

其實我正在創建一個應用程序,當開關打開時阻止來電。我用完整的代碼發佈了一個問題。請看看: - http://stackoverflow.com/questions/26673533/how-to-block-incoming-call-when-switch-widget-is-on – 2014-10-31 14:20:22

1

訪問swi的值TCH,你需要做到以下幾點:

((Switch) findViewById(R.id.switch_id)).isChecked(); 

但在廣播接收器的背景下,你沒有真正進入到佈局,所以你不能訪問交換機。 您必須在充滿具有開關的佈局的活動內執行此操作。

您可能在一個Activity中以編程方式註冊了BroadcastReceiver,這是我看到這個概念混合使用的唯一方式。

+0

其實,我正在創建一個應用程序,可在開關打開時阻止來電。我用完整的代碼發佈了一個問題。請看看: - http://stackoverflow.com/questions/26673533/how-to-block-incoming-call-when-switch-widget-is-on – 2014-10-31 14:20:43

0

您可以將該值保存在首選項中。 下面類將讓您輕鬆保存數據,並從首

retrive它
public class SessionManager { 

    private SharedPreferences pref; 
    private static SessionManager sessionManager; 




    public static SessionManager getInstance(Context context) { 
     if(sessionManager == null){ 
      sessionManager = new SessionManager(context); 
     } 
     return sessionManager; 
    } 


    public SessionManager(Context context) { 
     String PREF_NAME = context.getResources().getString(R.string.app_name); 
     this.pref = context.getSharedPreferences(PREF_NAME,Context.MODE_PRIVATE); 

    } 


    /** 
    * Getting value for key from shared Preferences 
    * 
    * @param key   key for which we need to get Value 
    * @param defaultValue default value to be returned if key is not exits 
    * @return It will return value of key if exist and defaultValue otherwise 
    */ 
    public String getValueFromKey(String key, String defaultValue) { 
     if (pref.containsKey(key)) { 
      return pref.getString(key, defaultValue); 
     } else { 
      return defaultValue; 
     } 
    } 


    /** 
    * Setting value for key from shared Preferences 
    * 
    * @param key key for which we need to get Value 
    * @param value value for the key 
    */ 
    public void setValueFromKey(String key, String value) { 
     pref.putString(key, value).apply(); 
    } 


    /** 
    * Setting value for key from shared Preferences 
    * 
    * @param key key for which we need to get Value 
    * @param value value for the key 
    */ 
    public void setFlagFromKey(String key, boolean value) { 
     pref.putBoolean(key, value).apply(); 
    } 


    /** 
    * To get Flag from sharedPreferences 
    * 
    * @param key key of flag to get 
    * @return flag value for key if exist. false if not key not exist. 
    */ 
    public boolean getFlagFromKey(String key) { 
     return pref.containsKey(key) && pref.getBoolean(key, false); 
    } 

} 
相關問題