本篇主要是关于SharedPreferences的几种访问MODE的学习
MODE主要是用来限制此Application与其他Application的之间是否能相互想问SharedPreferences的问题。
注意:第一次运行一个SharedPreference的Application,在 data/data/包名/下面有两个lib ,cache,并没有shared_prefs这个文件夹,当程序运行了editor.commit();只有当代码真正执行到editor.commit();提交的时候,之后,才会生成shared_prefs这个文件夹,里面有一个xml文件。
用下面的例子来说明:
工程1:SharedPreferenceTest1
在src下面包含两个包,com.myshared.test1;com.myshared.test111
(用于测试同一个Application下,其他包下面的类访问这个Application的SharedPreferences的MODE权限情况)
结论:不管创建SharedPreferences时候,用的什么模式,同一个Application下面的所有包的class都能对这个SharedPreferences进行edit,包括put,get等。
com.myshared.test1下:
SharedPreferenceTest1Activity
package com.myshared.test1;
import com.myshared.test111.SharedPreferenceTest111Activity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.view.View;
import android.view.View.OnClickListener;
import android.util.Log;
public class SharedPreferenceTest1Activity extends Activity {
/** Called when the activity is first created. */
private static String TAG = "SharedPreferenceTest1Activity";
private Button btn1;
private Button btn2;
private Button btn3;
private EditText name;
private EditText psw;
public SharedPreferences sp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
name = (EditText) findViewById(R.id.ed1);
psw = (EditText) findViewById(R.id.ed2);
btn1.setOnClickListener(myListener);
btn2.setOnClickListener(myListener);
btn3.setOnClickListener(myListener);
}
private OnClickListener myListener = new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub‘
if (v.getId() == R.id.btn1) {
// 1、获取SharedPreferences
/* SharedPreferences mysp = getSharedPreferences("susushared",
MODE_PRIVATE);*/
SharedPreferences mysp = getSharedPreferences("susushared",
Context.MODE_WORLD_READABLE);
// 2、编辑这个preference
Editor editor = mysp.edit();
// 3、往这个preference里面put值
String nameStr = name.getText().toString();
String ageStr = psw.getText().toString();
editor.putString("name", nameStr);
editor.putString("psw", ageStr);
// 4、提交
editor.commit();
Log.v(TAG, "#######commit");
} else if (v.getId() == R.id.btn2) {
// 1、获取SharedPreferences中的数据
SharedPreferences mysp2 = getSharedPreferences("susushared",
MODE_PRIVATE);
/* SharedPreferences mysp2 = getSharedPreferences("susushared",
Context.MODE_WORLD_READABLE);*/
//Context.MODE_WORLD_WRITEABLE
String returnname = mysp2.getString("name", "");
String returnspsw = mysp2.getString("psw", "");
Log.v(TAG, "returnname==" + returnname + "returnspsw=="
+ returnspsw);
}else if(v.getId() == R.id.btn3){
Intent t = new Intent();
t.setClass(SharedPreferenceTest1Activity.this, SharedPreferenceTest111Activity.class);
//t.setAction("tiaodao111");
startActivity(t);
}
}
};
}
package com.myshared.test111;
//别忘了把这个import进来,否则访问不到R
import com.myshared.test1.R;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.widget.Button;
//import android.widget.EditText;
import android.view.View;
import android.view.View.OnClickListener;
import android.util.Log;
public class SharedPreferenceTest111Activity extends Activity {
/** Called when the activity is first created. */
private static String TAG = "SharedPreferenceTest1Activity";
private Button btn3;
// private SharedPreferences sp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
btn3 = (Button) findViewById(R.id.btn4);
btn3.setOnClickListener(myListener);
}
private OnClickListener myListener = new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub‘
if (v.getId() == R.id.btn4) {
// 1、获取SharedPreferences
SharedPreferences mysp = getSharedPreferences("susushared",
MODE_PRIVATE);
Editor editor = mysp.edit();
editor.putString("name", "srx");
editor.putString("psw", "222");
editor.commit();
String name1 = mysp.getString("name", "");
String psw1 = mysp.getString("psw", "");
Log.v(TAG, "#######name1==" + name1 + "psw1==" + psw1);
}
}
};
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<TextView
android:id="@+id/tx1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/user" />
<EditText
android:id="@+id/ed1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tx2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/psw" />
<EditText
android:id="@+id/ed2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn2" />
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn3" />
</LinearLayout>
main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn4" />
</LinearLayout>
在AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myshared.test1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".SharedPreferenceTest1Activity"
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.myshared.test111.SharedPreferenceTest111Activity">
<intent-filter>
<action android:name="tiaodao111"/>
</intent-filter>
</activity>
</application>
</manifest>
在SharedPreferenceTest1Activity中,在R.id.btn1中,
不管创建时候的模式是什么,SharedPreferences mysp = getSharedPreferences("susushared",
MODE_XXXX);
不管这个MODE是什么,另外一个包的SharedPreferenceTest111Activity也可以对这个“susushared”进行edit,put,get操作。
工程2:
SharedPreferenceTest2:
SharedPreferenceTest2Activity
package com.myshared.test2;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SharedPreferenceTest2Activity extends Activity {
/** Called when the activity is first created. */
public SharedPreferences sp;
private Button btn2;
private static String TAG = "SharedPreferenceTest2Activity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn2 = (Button) findViewById(R.id.btn2);
btn2.setOnClickListener(myListener);
}
private OnClickListener myListener = new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub‘
if (v.getId() == R.id.btn2) {
/*
* 这样是给SharedPreferenceTest2Activity创建了一个名为susushared的SharedPreference
*/
/* // 1、获取SharedPreferences
SharedPreferences mysp =getSharedPreferences("susushared", MODE_PRIVATE);
String nameddd =mysp.getString("name", "");
String pswddd=mysp.getString("psw", ""); Log.v(TAG,
"#######nameddd=="+nameddd+"pswddd=="+pswddd);
// 2、编辑这个preference
Editor editor = mysp.edit();
// 3、往这个preference里面put值
editor.putString("name", "zf");
editor.putString("psw","zf");
// 4、提交
editor.commit();
Log.v(TAG,"#######22222commit");
*/
// 这样是从这个Application访问另外一个Application的SharedPreferences。
Context otherAppContext;
try {
otherAppContext = createPackageContext(
"com.myshared.test1",
Context.CONTEXT_IGNORE_SECURITY);
SharedPreferences otherPreferences = otherAppContext
.getSharedPreferences("susushared",
Context.MODE_WORLD_READABLE);
String nameddd = otherPreferences.getString("name", "");
String pswddd = otherPreferences.getString("psw", "");
Log.v(TAG, "#######nameddd==" + nameddd + "pswddd=="
+ pswddd + ", otherAppContext=" + otherAppContext);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
}
步骤:(1)createPackageContext()方法访问到对应你想访问的SharedPreferences所在的包,
(2)SharedPreferences otherPreferences = otherAppContext
.getSharedPreferences("susushared",
Context.MODE_WORLD_READABLE);
用得到的Context来得到对应的SharedPreferences,访问到名字为susushared的这个SharedPreferences,注意:工程1一定要把MODE设置成Context.MODE_WORLD_READABLE,否则得到的String就是默认放进去的""读出来,如下:
String nameddd = otherPreferences.getString("name", "");
String pswddd = otherPreferences.getString("psw", "");
在工程1里面存入数据,在工程2里面就可以读了,
存在的问题:工程2读到的工程1的数据不是实时的,当工程1对SharedPreferences的数据进行修改以后,工程2读到的还是原来的数据,在DDMS里面把工程2的包stop之后,然后再启动工程2,读到的就是工程1的SharedPreferences里面存的最新的数据。
不能实时更新,不能理解。