This article introduced the Sharedpreferences to save the application data the concrete step, for everybody reference, the concrete content is as follows
1, Sharedpreferences Simple Introduction
when the application is running, the user's configuration information may be maintained as the user uses it, such as the EQ setting at the last play, the volume setting, the information on the Internet cookies, and so on, which can be maintained by Sharedpreferences. The data persisted through Sharedpreferences is an XML file that is located in the application's private folder.
2, the concrete operation method
to get sharedpreferences, you can get the following methods:
Parameter brief:
After the name———— gets sharedpreferences, an XML file is saved in the application's private folder, and the first parameter name is the file name.
Mode ———— The save pattern for the XML file, which defaults to 0, which is mode_private
3, a simple demo
A music play example via service, "play" and "Pause" two buttons
After the pause, keep the playback progress to the sharedpreferences inside, and then play again, read the progress value for music playback.
* * @author: Conowen * @date: 12.3.01 * */package com.conowen.sharedpreferences;
Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button; The public class Sharedpreferencesactivity extends activity {/** called the ' when ' is the ' The activity ' is the ' the '---' @Overrid
e public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Button play = (button) Findviewbyid (R.id.play);
Button pause = (Button) Findviewbyid (R.id.pause);
Final Intent Intent = new Intent (sharedpreferencesactivity.this,service.class); The definition intent is final, global variable, for the following two anonymous inner classes (onclicklisenter) using Play.setonclicklistener (new Onclicklistener () {@Ove
Rride public void OnClick (View v) {//TODO auto-generated Method stub startservice (intent); //Start playing, through the previously defined Intent Pass}}); Pause.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {//T
Odo auto-generated Method Stub stopservice (intent);
Stop playback}}); }
}
The second class is inherited service, remember in manifest. Inside XML Register Service
* * @author: Conowen * @date: 12.3.01 * */package com.conowen.sharedpreferences;
Import Android.app.Service;
Import android.content.Intent;
Import android.content.SharedPreferences;
Import Android.media.MediaPlayer;
Import Android.os.IBinder;
public class Service extends service {MediaPlayer player;
New player @Override public void OnCreate () {//TODO auto-generated Method Stub super.oncreate ();
Player = Mediaplayer.create (this, r.raw.lt26);
Player.setlooping (TRUE); The first lifecycle of both methods is OnCreate ()} @Override public void OnStart (Intent Intent, int startid) {//TODO Auto-gen
erated method Stub Super.onstart (intent, Startid);
Sharedpreferences sp = this.getsharedpreferences ("music_progress", mode_private);
Music_progress is the filename of the XML file Player.seekto (Sp.getint ("Progress", 0)); Progress is obtained by player.getcurrentposition () for the key,progress value in the key-value pair, see below for details//each time the OnStart method is invoked, the player'sProgress//MediaPlayer Seekto method, read progress, and then play, getint the first parameter is the key name to read, the second parameter is the default initial value.
In other words, the initial progress defaults to 0 player.start ();
@Override public void OnDestroy () {//TODO auto-generated Method Stub Super.ondestroy ();
Sharedpreferences sp = this.getsharedpreferences ("music_progress", mode_private);
Sp.edit (). Putint ("Progress", Player.getcurrentposition ()). commit ();
Player.getcurrentposition () method to obtain the playback progress of the data//commit () method is to save the sharedpreferences obtained data player.stop ();
@Override public IBinder onbind (Intent Intent) {//TODO auto-generated method stub return null; }
}
The documents saved by Sharedpreferences are shown below, open DDMS, switch to file Explore, and in the Shared_prefs folder in the private directory, the path is
/data/data/Your bag name/shared_prefs
The contents are as follows
<?xml version= ' 1.0 ' encoding= ' utf-8 ' standalone= ' yes '?>
<map> <int name= ' progress ' value= '
90695 "/>
This is the entire content of this article, I hope to learn more about Android software programming help.