• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.apis.preference;
18 
19 import com.example.android.apis.R;
20 
21 import android.content.Context;
22 import android.content.SharedPreferences;
23 import android.os.Bundle;
24 import android.preference.PreferenceActivity;
25 import android.preference.PreferenceManager;
26 
27 /**
28  * This activity is an example of a simple settings screen that has default
29  * values.
30  * <p>
31  * In order for the default values to be populated into the
32  * {@link SharedPreferences} (from the preferences XML file), the client must
33  * call
34  * {@link PreferenceManager#setDefaultValues(android.content.Context, int, boolean)}.
35  * <p>
36  * This should be called early, typically when the application is first created.
37  * An easy way to do this is to have a common function for retrieving the
38  * SharedPreferences that takes care of calling it.
39  */
40 public class DefaultValues extends PreferenceActivity {
41     // This is the global (to the .apk) name under which we store these
42     // preferences.  We want this to be unique from other preferences so that
43     // we do not have unexpected name conflicts, and the framework can correctly
44     // determine whether these preferences' defaults have already been written.
45     static final String PREFS_NAME = "defaults";
46 
47     @Override
onCreate(Bundle savedInstanceState)48     protected void onCreate(Bundle savedInstanceState) {
49         super.onCreate(savedInstanceState);
50 
51         getPrefs(this);
52         getPreferenceManager().setSharedPreferencesName(PREFS_NAME);
53         addPreferencesFromResource(R.xml.default_values);
54     }
55 
getPrefs(Context context)56     static SharedPreferences getPrefs(Context context) {
57         PreferenceManager.setDefaultValues(context, PREFS_NAME, MODE_PRIVATE,
58                 R.xml.default_values, false);
59         return context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
60     }
61 }
62