我们可以在一个应用程序中创建并编辑一个Preferences ,然后在另外一个应用程序中读取它。当然有个前提是该Preferences 的权限至少是Context.MODE_WORLD_READABLE

 

比如在包名为com.teleca 应用程序A中有一个名叫com.teleca_prefer的Preferences:

final static String preferName="com.teleca_prefer";
final static String KEY_TIP="tip";


prefsWorldRead = getSharedPreferences(preferName, Context.MODE_WORLD_READABLE);
Editor prefsWorldReadEditor = prefsWorldRead.edit();
prefsWorldReadEditor.putString(KEY_TIP, "Are you fine?");
prefsWorldReadEditor.commit();

 

我们可以在包名为com.teleca.robin应用程序B中这样读取它:

private SharedPreferences prefsWorldRead;
final static String preferName="com.teleca_prefer";
final static String KEY_TIP="tip";

if(prefsWorldRead ==null) {
    Context otherContext=null;
    try {
        otherContext =createPackageContext("com.teleca", Context.CONTEXT_IGNORE_SECURITY );
    } catch (NameNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    prefsWorldRead = otherContext.getSharedPreferences(preferName, Context.MODE_WORLD_READABLE);
}
String tip=prefsWorldRead.getString(KEY_TIP, "null2");

 

createPackageContext为Context的方法,"com.teleca"为A应用程序的包名,"com.teleca_prefer"为你要读取的A应用程序的Preferences名字。