我们可以在一个应用程序中创建并编辑一个Preferences,然后在另外一个应用程序中读取它。当然有个前提是该 Preferences的权限至少是Context.MODE_WORLD_READABLE。
prefsWorldRead = getSharedPreferences(preferName,
Context.MODE_WORLD_READABLE)
;
Editor prefsWorldReadEditor = prefsWorldRead.edit();
prefsWorldReadEditor.putString(KEY_TIP, "Are you fine?");
prefsWorldReadEditor.commit();
if(prefsWorldRead ==null)
{
Context otherContext=null;
try {
prefsWorldRead = otherContext.getSharedPreferences(preferName,
Context.MODE_WORLD_READABLE);
}
String tip=prefsWorldRead.getString(KEY_TIP, "null2");
public abstract
比如在包名为
com.teleca
应用程序A中有一个名叫
com.teleca_prefer
的Preferences:
final static String preferName="
com.teleca_prefer
";
final static String KEY_TIP="tip";
.......................................................
我们可以在包名为com.teleca.robin应用程序B中这样读取它:
private SharedPreferences prefsWorldRead;
final static String preferName="
com.teleca_prefer
";
final static String KEY_TIP="tip";
....................................................................
otherContext =createPackageContext("
com.teleca
",
Context.CONTEXT_IGNORE_SECURITY
);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
createPackageContext
为Context的方法,
"
com.teleca
"
为A应用程序的包名,
"
com.teleca_prefer
"
为你要读取的A应用程序的
Preferences
名字。
public abstract Context createPackageContext (String packageName, int flags)
Since:
API Level 1
Return a new Context object for the given application name. This Context is the same as what the named application gets when it is launched, containing the same resources and class loader. Each call to this method returns a new instance of a Context object; Context objects are not shared, however they share common state (Resources, ClassLoader, etc) so the Context instance itself is fairly lightweight.
Throws PackageManager.NameNotFoundException
Throws SecurityException
CONTEXT_INCLUDE_CODE
Parameters
packageName | Name of the application's package. |
---|---|
flags | Option flags, one of CONTEXT_INCLUDE_CODE CONTEXT_IGNORE_SECURITY . |
Throws
| java.lang.SecurityException |
---|---|
| if there is no application with the given package name |
PackageManager.NameNotFoundException |