我正在尝试从其他应用程序访问在一个应用程序中创建的共享首选项值只有当我从任务管理器关闭应用程序时才能检索数据,否则更改不会反映。
第一个活动:
btnSignIn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// get The User name and Password
String userName = editTextUserName.getText().toString();
String password = editTextPassword.getText().toString();
sharedPreferences = getSharedPreferences(PREFS_READ_WRITE, Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);
Editor prefsPrivateEditor = sharedPreferences.edit();
prefsPrivateEditor.putString(KEY_READ_WRITE, "test");
prefsPrivateEditor.commit();
To clear:
clear.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
sharedPreferences = getSharedPreferences(PREFS_READ_WRITE, Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);
Editor prefsPrivateEditor = sharedPreferences.edit();
prefsPrivateEditor.clear();
prefsPrivateEditor.commit();
}
});
在其他应用程序中,我正在检索这样的:
Context otherAppsContext = null;
try {
otherAppsContext = createPackageContext("com.example.app", 0);
} catch (NameNotFoundException e) {
}
sharedPreferences = otherAppsContext.getSharedPreferences(PREFS_READ_WRITE, Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);
String authToken=sharedPreferences.getString(KEY_READ_WRITE, "WORLD READ WRITE EMPTY");
Toast.makeText(getApplicationContext(), authToken, Toast.LENGTH_SHORT).show();