I need visit the sharedPreference of another application, the SharedPreference have to set
"MODE_WORLD_WRITEABLE|MODE_WORLD_READABLE" , and show you the Logcat.
03-23 13:54:45.038: E/ApplicationContext(10895): Couldn't rename file /data/data/com.mzw.gamehelper/shared_prefs/public.xml to backup file /data/data/com.mzw.gamehelper/shared_prefs/public.xml.bak
I dont know what's wrong with it, who can help me, thank you.
try this code insert values
private SharedPreferences myPrefs;
myPrefs = Actionactivity.this.getSharedPreferences("myPrefs", MODE_WORLD_WRITEABLE);
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putString("Mobile_no", getText_no.getText().toString().trim());
prefsEditor.commit();
and try this code get values
myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
myPrefs.getString("Mobile_no", "");
TRY THIS WAY ::
Context otherAppsContext = null;
private SharedPreferences sharedPreferences;
public static final String PREFS_READ = "PREFS_READ";
public static final String KEY_READ = "KEY_READ";
try {
//com.dfdf.fdfdf.android.sharedpreferences OTHER APPLICTION PACKAGE NAME
otherAppsContext = createPackageContext("com.dfdf.fdfdf.android.sharedpreferences", 0);
} catch (NameNotFoundException e) {
}
sharedPreferences = otherAppsContext.getSharedPreferences(PREFS_READ, Context.MODE_WORLD_WRITEABLE);
String strvalue=sharedPreferences.getString(KEY_READ, "WORLD READ EMPTY")
Related
i am trying to pass a string set between activities by sharedpreferences but it seems that the default sharedpreferences made two files for each activity.
i tried to share it with PRIVATE_MODE with the same name and it didnt work
SharedPreferences appPrefernces = PreferenceManager.getDefaultSharedPreferences(ctx);
SharedPreferences.Editor appEditor = appPrefernces.edit();
Set<String> usersSet = appPrefernces.getStringSet("users", new HashSet<String>());
if(!usersSet.contains(id)) {
usersSet.add(id);
appEditor.putStringSet("users", usersSet);
appEditor.apply();
}
SharedPreferences appPrefernces = PreferenceManager.getDefaultSharedPreferences(Main.this);
users = appPrefernces.getStringSet("users",new HashSet<String>());
it seems that it saved the info but while extracting it i get a get just a partial set
In this way you are sure what preferences file you are using:
private static void saveUsers(Context context, Set<String> usersSet) {
final SharedPreferences sharedPreferences = context.getSharedPreferences("preferences", Context.MODE_PRIVATE);
final SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putStringSet("users", userSet).apply();
}
private static Set<String> loadUsers(Context context){
final SharedPreferences sharedPreferences = context.getSharedPreferences("preferences", Context.MODE_PRIVATE);
return sharedPreferences.getStringSet("users", new HashSet<String>());
}
Hope it helps.
I am not able to access shared preference data. I have already checked in another URL for this error. Error is...
remove failed: ENOENT (No such file or directory) : /data/data/com.example.lenovo.sms/shared_prefs/refresh_token.xml.bak
Code for storing and accessing data in shared preference
private void saveToken(String recent_token) {
saveTokenPref = getSharedPreferences(Config.SAVE_TOKEN_FILE,Context.MODE_PRIVATE);
SharedPreferences.Editor editor = saveTokenPref.edit();
editor.putString(Config.TAG_TOKEN,recent_token);
editor.commit();
}
public String getSavedToken() {
saveTokenPref = getSharedPreferences(Config.SAVE_TOKEN_FILE,Context.MODE_PRIVATE);
String saved_token = saveTokenPref.getString(Config.TAG_TOKEN,"");
Log.d("save token",saved_token);
return saved_token;
}
In onCreate():
saveTokenPref = PreferenceManager.getDefaultSharedPreferences(this);
Then, use the below codes:
private void saveToken(String recent_token) {
SharedPreferences.Editor editor = saveTokenPref.edit();
editor.putString("token",recent_token);
editor.commit();
}
public String getSavedToken() {
String saved_token = saveTokenPref.getString("token","");
Log.d("save token",saved_token);
return saved_token;
}
Hope this helps.
I'm using preferences to save some user settings in my Nexus 7 app. My code for saving a value to preferences is:
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
String systemId = spinnerActivity.getSelectedItem().toString();
editor.putString(PreferenceKeys.SAVED_SYSTEMID, systemId);
if (!editor.commit()) {
Toast.makeText(getApplicationContext(), "Error saving System ID", Toast.LENGTH_LONG).show();
}
I've stepped through this with the debugger and it is being called correctly. When I restart my app. and try to read back the value with the code below, I always get null.
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
String savedSystemId = sharedPref.getString(PreferenceKeys.SAVED_SYSTEMID, null);
ConnectionInfo.setSystemId(savedSystemId);
The loading is called from the onCreate() function in the main activity. Strangely enough loading of other preference values elsewhere in the app. works fine, it's just this one case that doesn't work. Can anyone see what's wrong?
SharedPreferences sharedPref = getSharedPreferences("Name_of_item",Context.MODE_PRIVATE);
please try the below
SharedPreferences sharedPref = getPreferences("preference_name",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
String systemId = spinnerActivity.getSelectedItem().toString();
editor.putString(PreferenceKeys.SAVED_SYSTEMID, systemId);
if (!editor.commit()) {
Toast.makeText(getApplicationContext(), "Error saving System ID", Toast.LENGTH_LONG).show();
}
SharedPreferences sharedPref = getPreferences("preference_name",Context.MODE_PRIVATE);
String savedSystemId = sharedPref.getString(PreferenceKeys.SAVED_SYSTEMID, null);
ConnectionInfo.setSystemId(savedSystemId);
Instead of using:
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
Try to use this:
import android.preference.PreferenceManager;
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
Or, you can use a named preferences page in which case:
public static final String PREF_FILE_NAME = "PrefFile";
SharedPreferences sharedPref = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);
I am building a camera application. I'm trying to save some information using SharedPreferences.
I want to save some persistant information-the last image taken filepath. But the first time the application is used before taking a picture, the data would be NULL.
So I want to getSharedPreferences in onCreate and check if the value is null. But as far as I know the only way to use getSharedPreferences is only if you have called put on the Editor before. Hence, I am getting a NULL pointer exception on the SharedPreferences object the first time.
How do you resolve this?
//inside on Create()
imageData = getSharedPreferences("ImageData",MODE_PRIVATE);
SharedPreferences.Editor prefEditor = imageData.edit();
prefEditor.commit();
String previousImage = imageData.getString("lastImageTaken", null);
if(previousImage == null){
Log.d(TAG,"previous image is NULL");
}
else{
//do something with the filepath
}
//-----------------------
//in onClick of capture button
imageData = getSharedPreferences("ImageData",MODE_PRIVATE);
SharedPreferences.Editor prefEditor = imageData.edit();
prefEditor.putString("lastImageTaken", MyActivity.this.pictureFilePath);
prefEditor.commit();
Please try this
To read from SharedPreferences
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
String name = preferences.getString("name","default");
String email = preferences.getString("email","default");
To save into SharedPreferences
Editor edit = preferences.edit();
edit.putString("name", "Roy");
edit.putString("email", "roy#mail.com");
edit.commit();
I want to store the hashmap object in global class so that it will store value even after the mobile restart. Any idea how to go about this concept.
serialize your hashmap object before restarting and deserialize it after restart...
here is sample code for serialization..
public void serializeMap(HashMap<String,String> hm) {
try {
FileOutputStream fStream = openFileOutput(namefile.bin, Context.MODE_PRIVATE) ;
ObjectOutputStream oStream = new ObjectOutputStream(fStream);
oStream.writeObject(hm);
oStream.flush();
oStream.close();
Log.v("Serialization success", "Success");
} catch (Exception e) {
Log.v("IO Exception", e.getMessage());
}
}
you can similarly read it by deserializing it....
Thanks....
Thanks very much but same thing can be done using the shared Preferences technique.
Below is the code to add data into shared preferences and check if already exists.
SharedPreferences preferences = getSharedPreferences(
PREF_FILE_NAME, MODE_PRIVATE);
if (value.equals("")) {
boolean storedPreference = preferences.contains(key);
if (storedPreference) {
SharedPreferences.Editor editor = preferences.edit();
editor.remove(key); // value to store
Log.d("KEY",key);
editor.commit();
}
}else{
SharedPreferences.Editor editor = preferences.edit();
editor.putString(key, value); // value to store
Log.d("KEY",key);
editor.commit();
}
then we can access using the
SharedPreferences preferences = getSharedPreferences(
PREF_FILE_NAME, MODE_PRIVATE);
Map<String, String> map = (Map<String, String>) preferences.getAll();
if(!map.isEmpty()){
Iterator<Entry<String, String>> iterator = map.entrySet().iterator();
while(iterator.hasNext()){
Map.Entry pairs = (Map.Entry)iterator.next();
pairs.getKey()+pairs.getValue();
//write code here
}
}
Serialize it and save it in shared preferences or in a file. Whether you can do this, of course, depends on the data types being mapped from and to. (This won't work, for instance, if you try to serialize a View.)