Monday, 12 August 2013

Not able to access/clear Sharedprefrence value from other app

Not able to access/clear Sharedprefrence value from other app

I'm trying to access shared preference value created in one application
from other applications i'm able to retrieve the data only if i close the
app from the task manager else the changes are not reflecting.Please tell
me where i'm going wrong.
First Activty:
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();
}
});
In other App i'm retrieving like this :
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();

No comments:

Post a Comment