sharedpreferences 用于保存少量数据,格式简单的entry对,各种配置信息等。
sharedpreferences接口主要负责读取应用中preferences数据,调用edit()方法 返回editor对象,而后进行相应的写入数据。
context给我们提供了getSharedPreferences(String name,int mode)方法返回sharedpreferences实例。
数据默认保存在/data/data/。。。。packagename。。。。/shared_prefs目录下,以xml形式存在。
找到目录导出 查看如下格式、结果;
一、在当前应用中
java代码实现-----------------------------------------------------------------------------------------------
================================================================
对应的xml布局------------------------------------------------------------------------------------------------------
======================================================================
二、跨应用Sharedpreferences
1、 创建数据共享的具体环境(报名作为一个程序的标识)
Context context= context.CreatePackageContext(包名 , 环境权限);
2、用具体标识环境调用相应的方法,如产生实例等,操作共享数据。
-------------------------------------------------------------------------------------------------------------------
==================================================================
sharedpreferences接口主要负责读取应用中preferences数据,调用edit()方法 返回editor对象,而后进行相应的写入数据。
context给我们提供了getSharedPreferences(String name,int mode)方法返回sharedpreferences实例。
数据默认保存在/data/data/。。。。packagename。。。。/shared_prefs目录下,以xml形式存在。
找到目录导出 查看如下格式、结果;
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="name">era </string>
<int name="age" value="522" />
</map>
一、在当前应用中
java代码实现-----------------------------------------------------------------------------------------------
================================================================
package com.baidu.preferences;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class PreferencesInActivity extends Activity {
private Button read, write;
private EditText age, name;
private SharedPreferences sp;
private SharedPreferences.Editor spEditor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preferences_in);
read = (Button) findViewById(R.id.read);
write = (Button) findViewById(R.id.write);
age = (EditText) findViewById(R.id.age);
name = (EditText) findViewById(R.id.name);
sp = getSharedPreferences("value", Context.MODE_WORLD_READABLE);
spEditor = sp.edit();
read.setOnClickListener(listen);
write.setOnClickListener(listener);
}
// 读取数据
OnClickListener listen = new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),
sp.getString("name", null) + sp.getInt("age", 0), 3000)
.show();
}
};
// 写入数据
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
int s = Integer.parseInt(age.getText().toString());
spEditor.putString("name", name.getText().toString());
spEditor.putInt("age", s);
spEditor.commit();
}
};
}
对应的xml布局------------------------------------------------------------------------------------------------------
======================================================================
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PreferencesInActivity" >
<Button
android:id="@+id/write"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/age"
android:layout_centerVertical="true"
android:layout_marginLeft="17dp"
android:text="写入" />
<Button
android:id="@+id/read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/write"
android:layout_alignRight="@+id/age"
android:layout_marginRight="20dp"
android:text="读取" />
<EditText
android:id="@+id/age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/write"
android:layout_centerHorizontal="true"
android:layout_marginBottom="42dp"
android:inputType="number"
android:singleLine="true"
android:ems="10"
android:hint="请输入年龄" >
</EditText>
<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/age"
android:layout_alignLeft="@+id/age"
android:singleLine="true"
android:layout_marginBottom="18dp"
android:ems="10"
android:hint="请输入名字" />
</RelativeLayout>
二、跨应用Sharedpreferences
1、 创建数据共享的具体环境(报名作为一个程序的标识)
Context context= context.CreatePackageContext(包名 , 环境权限);
2、用具体标识环境调用相应的方法,如产生实例等,操作共享数据。
-------------------------------------------------------------------------------------------------------------------
==================================================================
package com.baidu.preferencesout;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class PreferencesSharedActivity extends Activity {
private Button readOut;
private SharedPreferences sp;
private Context con;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preferences_shared);
readOut = (Button) findViewById(R.id.readOut);
try {
con = createPackageContext("com.baidu.preferences",
Context.CONTEXT_IGNORE_SECURITY);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
readOut.setOnClickListener(l);
}
OnClickListener l = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
sp = con.getSharedPreferences("value", Context.MODE_WORLD_READABLE);
Toast.makeText(
getApplicationContext(),
sp.getString("name", null)
+ sp.getInt("age", 0), 3000).show();
}
};
}