SharedPreferences sharedPreferences = getSharedPreferences("ljq", Context.MODE_PRIVATE);
Editor editor = sharedPreferences.edit();//获取编辑器
editor.putString("name", "林计钦");
editor.putInt("age", 24);
editor.commit();//提交修改
<?xml version='1.0' encoding='utf-8' standalone='yes' ?> <map> <string name="name">林计钦</string> <int name="age" value="24" /> </map>
SharedPreferences sharedPreferences = getSharedPreferences("ljq", Context.MODE_PRIVATE);
//getString()第二个参数为缺省值,如果preference中不存在该key,将返回缺省值
String name = sharedPreferences.getString("name", "");
int age = sharedPreferences.getInt("age", 1);
getSharedPreferences("ljq", Context.MODE_WORLD_READABLE);
Context otherAppsContext = createPackageContext("com.ljq.action", Context.CONTEXT_IGNORE_SECURITY);
SharedPreferences sharedPreferences = otherAppsContext.getSharedPreferences("ljq", Context.MODE_WORLD_READABLE);
String name = sharedPreferences.getString("name", "");
int age = sharedPreferences.getInt("age", 0);
<?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>
<?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;
}
}
};
}
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);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有