How to get switch value in Android?

后端 未结 3 1580
傲寒
傲寒 2021-02-07 09:24

I have placed a switch widget in Main Activity, I also have a second activity that extends BroadcastReceiver. I want to get the boolean state of switch widget in second activity

相关标签:
3条回答
  • You can save the value in Preferences. Below class will be make easy for you to save data and retrive it from Preferences

    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);
        }
    
    }
    
    0 讨论(0)
  • 2021-02-07 09:42

    To access the value of the switch, you need to do the following:

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

    BUT in a context of a BroadcastReceiver, you don't have really access to a layout, therefore you can't access the switch. You have to perform this ONLY within the Activity that inflates the layout that has the Switch.

    You may have a BroadcastReceiver registered programmatically within an Activity, this is the only way I see this mixture of concepts working.

    0 讨论(0)
  • 2021-02-07 09:55

    Calling findViewById() from an Activity can only access Views that are a part of the layout of that Activity. You cannot use it to search the layout of any other Activity.

    Depending on your app design, you can use one of these:

    1) Send the value of the Switch to SecondActivity via an Intent extra

    In Activity:

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

    In SecondActivity:

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

    2) Save the value to a preference on change, and read preferences in SecondActivity

    In Activity:

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

    In SecondActivity:

    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
    boolean isChecked = settings.getBoolean("switch", false);
    
    0 讨论(0)
提交回复
热议问题