User preferences file vs App preferences file

后端 未结 3 1710
情深已故
情深已故 2020-12-16 03:07

My android application has two kinds of preferences:

1) I have user preferences defined in res/xml/preferences.xml so that users can manage their pr

3条回答
  •  不思量自难忘°
    2020-12-16 04:04

    Let us assume that your xml reads like this:

    
    
       
    

    I assume you use a PreferenceActivity to load the preferences from your xml. So in your activity you do addPreferencesFromResource(R.xml.application_pref);

    When you do this, by default, all the values are stored in the default SharedPreference of the application. You can access these preferences in anywhere you want. So from some other activity/service just do:

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    boolean value = prefs.getBoolean("gpsOn", false);
    

    And just like that you should get the user's preference in value.

提交回复
热议问题