代码如下:
?xml version="1.0" encoding="utf-8"?
resources
string name="hello"Hello World, SpActivity!/string
string name="app_name"软件配置参数/string
string name="name"姓名/string
string name="age"年龄/string
string name="button"保存设置/string
string name="showButton"显示/string
/resources
main.xml布局文件
代码如下:
?xml version="1.0" encoding="utf-8"?
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"
android:textSize="20px"
android:id="@+id/nameLable" /
EditText android:layout_width="80px"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/nameLable"
android:layout_alignTop="@id/nameLable"
android:layout_marginLeft="10px"
android:id="@+id/name" /
/RelativeLayout
RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20px"
android:text="@string/age"
android:id="@+id/ageLable" /
EditText android:layout_width="80px"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/ageLable"
android:layout_alignTop="@id/ageLable"
android:layout_marginLeft="10px"
android:id="@+id/age" /
/RelativeLayout
RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"
android:id="@+id/button" /
Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/showButton"
android:layout_toRightOf="@id/button"
android:layout_alignTop="@id/button"
android:id="@+id/showButton" /
/RelativeLayout
TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20px"
android:id="@+id/showText" /
/LinearLayout
代码如下:
package com.ljq.activity;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class SpActivity extends Activity {
private EditText nameText;
private EditText ageText;
private TextView resultText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
nameText = (EditText)this.findViewById(R.id.name);
ageText = (EditText)this.findViewById(R.id.age);
resultText = (TextView)this.findViewById(R.id.showText);
Button button = (Button)this.findViewById(R.id.button);
Button showButton = (Button)this.findViewById(R.id.showButton);
button.setOnClickListener(listener);
showButton.setOnClickListener(listener);
// 回显
SharedPreferences sharedPreferences=getSharedPreferences("ljq123",
Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);
String nameValue = sharedPreferences.getString("name", "");
int ageValue = sharedPreferences.getInt("age", 1);
nameText.setText(nameValue);
ageText.setText(String.valueOf(ageValue));
}
private View.OnClickListener listener = new View.OnClickListener(){
public void onClick(View v) {
Button button = (Button)v;
//ljq123文件存放在/data/data/package name/shared_prefs目录下
SharedPreferences sharedPreferences=getSharedPreferences("ljq123",
Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);
switch (button.getId()) {
case R.id.button:
String name = nameText.getText().toString();
int age = Integer.parseInt(ageText.getText().toString());
Editor editor = sharedPreferences.edit(); //获取编辑器
editor.putString("name", name);
editor.putInt("age", age);
editor.commit();//提交修改
Toast.makeText(SpActivity.this, "保存成功", Toast.LENGTH_LONG).show();
break;
case R.id.showButton:
String nameValue = sharedPreferences.getString("name", "");
int ageValue = sharedPreferences.getInt("age", 1);
resultText.setText("姓名:" + nameValue + ",年龄:" + ageValue);
break;
}
}
};
}
运行结果
如何访问其他应用中的Preference?
代码如下:
package com.ljq.sp;
import java.io.File;
import java.io.FileInputStream;
import android.content.Context;
import android.content.SharedPreferences;
import android.test.AndroidTestCase;
import android.util.Log;
public class AccessSharePreferenceTest extends AndroidTestCase{
private static final String TAG = "AccessSharePreferenceTest";
/**
* 访问SharePreference的方式一,注:权限要足够
* @throws Exception
*/
public void testAccessPreference() throws Exception{
String path = "/data/data/com.ljq.activity/shared_prefs/ljq123.xml";
File file = new File(path);
FileInputStream inputStream = new FileInputStream(file);
//获取的是一个xml字符串
String data = new FileService().read(inputStream);
Log.i(TAG, data);
}
/**
* 访问SharePreference的方式二,注:权限要足够
* @throws Exception
*/
public void testAccessPreference2() throws Exception{
Context context = this.getContext().createPackageContext("com.ljq.activity",
Context.CONTEXT_IGNORE_SECURITY);
SharedPreferences sharedPreferences = context.getSharedPreferences("ljq123",
Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);
String name = sharedPreferences.getString("name", "");
int age = sharedPreferences.getInt("age", 1);
Log.i(TAG, name + " : " +age);
}
}