Sharedpreferences in Android

Source: Internet
Author: User

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 are very clear at Android SDK website. I just copied here (I divided the contents to four parts):

Introduction

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

Get Sharedpreferences Object

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

    • getSharedPreferences()-If you need multiple preferences files identified by name, which your specify with the first parameter.
    • getPreferences()-Use the If you need only one preferences file for your Activity. Because This is the only preferences file for your Activity, and you don't supply a name.

Write the values

To write values:

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

Read the values

to read values, SharedPreferences use methods such getBoolean() as 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>

Activities exist in this example,

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

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

?
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", "[email protected]"));     } }

We set up the values and store them in Sharedpreferences object. These the values can be changed in Mainactivity.java, and would is displayed in Main.java.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.