SharedPreferences in Android

As far as I know, SharedPreferences allow us to store some simple data in a named area in our phone. Introduction to the uses of SharedPreferences is very clear at Android SDK website. I just copied here(I divided the contents into four parts):

Introduction

The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed).

Get SharedPreferences object

To get a SharedPreferences object for your application, use one of two methods:

  • getSharedPreferences() - Use this if you need multiple preferences files identified by name, which you specify with the first parameter.
  • getPreferences() - Use this if you need only one preferences file for your Activity. Because this will be the only preferences file for your Activity, you don‘t supply a name.

Write the values

To write values:

  1. Call edit() to get a SharedPreferences.Editor.
  2. Add values with methods such as putBoolean() and putString().
  3. Commit the new values with commit()

Read the values

To read values, use SharedPreferences methods such as getBoolean() and getString().

_______________________________________________________________________________________________________________________

Now let‘s get into example time: 

First, take a look at AndroidManifest.xml file to get to know it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!--  part of AndroidManifest.xml  -->
<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.sharedpreferences.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.sharedpreferences.Display"
            android:label="Display" >
        </activity>
    </application>

Two Activities exist in this example,

MainActivity.java which uses activity_main.xml as the content view; 

SharedPreferences in Android

SharedPreferences in Android

 

Display.java which uses main.xml as the content view:

SharedPreferences in Android

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<span style="color: rgb(153, 204, 0);"><strong>//MainActivity.java</strong></span><br>public class MainActivity extends Activity {
    private static final String MY_PREF="firstpreferencefile";
    private Button change;
    private EditText ed1;
    private EditText ed2;
    private SharedPreferences sp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        change=(Button)findViewById(R.id.change);
        ed1=(EditText)findViewById(R.id.editText1);
        ed2=(EditText)findViewById(R.id.editText2);
        sp=getSharedPreferences(MY_PREF, 0);
        System.out.println("hello"+sp.getString("name", "null"));
                 
        change.setOnClickListener(new View.OnClickListener() {
             
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                 
                Editor editor=sp.edit();
                editor.putString("name", ed1.getText().toString());
                editor.putString("email", ed2.getText().toString());
                editor.commit();
                Intent intent=new Intent(getApplicationContext(),Display.class);
                startActivity(intent);
            }
        });
         
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<span style="color: rgb(153, 204, 0);"><strong>//Display.java</strong></span>
public class Display extends Activity {
    private static final String MY_PREF="firstpreferencefile";
    private TextView tv1;
    private TextView tv2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        SharedPreferences sp=getSharedPreferences(MY_PREF, 0);
        tv1=(TextView)findViewById(R.id.textView1);
        tv2=(TextView)findViewById(R.id.textView2);
        tv1.setText(sp.getString("name", "song"));
        tv2.setText(sp.getString("email", "jies.2408@gmail.com"));
    }
}

  We set up two values and store them in SharedPreferences object. These two values can be changed in MainActivity.java, and would be displayed in main.java.

 

SharedPreferences in Android,布布扣,bubuko.com

SharedPreferences in Android

上一篇:oracle15 pl/sql 分页


下一篇:使用solr界面管理工具创建core 不能用的解决方法