Checkbox Preference and Checking if its enabled or disable

我的未来我决定 提交于 2019-12-10 13:58:28

问题


Im having issues with the below code on making a checkbox preference, That by default is checked. My first activity is a simple splash screen, and simply before show my imageview thread i want to check if the checkbox has been disable if so then i want to intent directly to the main activity and by default i show my image thread, or in reversed order of that.

Currently my splashscreen is launching no matter if its checked or now, Any help would be greatly appreciated

XML

 <CheckBoxPreference
      android:title="@string/category_tools_startupscreen"
      android:summary="@string/category_tools_startupscreen_summary"
      android:key="boot_animation" android:order="5" android:enabled="true"/>

SplashScreen

   SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);

    if (settings.getBoolean("boot_animation", true)) {
        setContentView(R.layout.splash_screen);
        Thread splashThread = new Thread() {
            @Override
            public void run() {
                try {
                    int waited = 0;
                    // changed from 5000 to 4000 11.29
                    while (waited < 3000) {
                        sleep(100);
                        waited += 100;
                    }
                } catch (InterruptedException e) {
                    // do nothing
                } finally {
                    Intent i = new Intent();
                    i.setClassName("com.example.app",
                            "com.example.app.MainActivity");
                    startActivity(i);
                    finish();
                }
            }
        };
        splashThread.start();
    }

    else {
        Intent i = new Intent();
        i.setClassName("com.example.app",
                "com.example.app.MainActivity");
        startActivity(i);
        finish();
    }

}

Settings

    final CheckBoxPreference checkboxPref2 = (CheckBoxPreference) getPreferenceManager().findPreference("boot_animation");

            checkboxPref2.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {            
                 public boolean onPreferenceChange(Preference preference, Object newValue) {
                if(newValue instanceof Boolean){
                        Boolean boolVal = (Boolean)newValue;
                        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                        SharedPreferences.Editor editor = settings.edit();
                        editor.putBoolean("checked", boolVal);
                        editor.commit();
                    }
                    return true;
                }
            }); 

回答1:


In your splash screen

PreferenceManager.setDefaultValues(this, R.xml.your_setting_xml, false);
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);

if (settings.getBoolean("boot_animation", true)) {
.........

You do not even need the code you posted for the Settings



来源:https://stackoverflow.com/questions/16955443/checkbox-preference-and-checking-if-its-enabled-or-disable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!