Read preference of other application android - android

i've read that shared preferences can be retrieved from outside my application .
this code did not work:
try
{
myContext = createPackageContext("com.intervigil.micdroid", Context.MODE_WORLD_WRITEABLE); // where com.example is the owning app containing the preferences
SharedPreferences testPrefs = myContext.getSharedPreferences("test_prefs", Context.MODE_WORLD_READABLE);
Map<String, ?> items = testPrefs.getAll();
nbenroullement= (Integer) items.get("enroullement");
System.out.println("*********************" + nbenroullement);
}
catch (NameNotFoundException e)
{
e.printStackTrace();
}
Please can any one help me . Thank you

Finaly i get the solution in this tutorial
I hope that i help you
http://androiddhamu.blogspot.in/2012/03/share-data-across-application-in.html

That's probably because you aren't itereting the Maps items.
So, try to do something like that :
Map<String, ?> items = testPrefs .getAll();
for(String s : items.keySet()){
String value = items.get(s).toString()); //this is the key of preferences
}
Once you retrieve the key (enroullement, I presume) you got to use it like this :
enroullement = Integer.valueOf(prefs.getString("enroullement", "0")); //0 is a default value
Hope it helps.

Related

sharedPreference can't get updated value from a service of another application

I have 2 different android applications (app1 and app2). In app2, I tried to get the value of the sharedPreference from a service of app1. I used the following codes:
In a service of app 1 :
sharedPreferences = getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove("currentKey");
editor.putString("currentKey",target);//update sharedPrerences
editor.apply();
Log.i(TAG,"value is" + sharedPreferences.getString("currentKey", null) );
And in app2:
try {
Context context = createPackageContext("com.example.packageName",0);
sharedPreferences = context.getSharedPreferences(PREFERENCES,Context.MODE_PRIVATE);
String current = sharedPreferences.getString("currentKey",null);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
Two apps have the same following info in the manifest:
android:sharedUserId="com.android.example"
android:sharedUserLabel="#string/user_id_label"
In app2, I can get the value of sharedPreference, but the problem is that the value isn't updated when the service of app1 changes value of the sharedPreference. Have no idea! If someone have fallen in the same case, please help me! Thanks a lot!

Sharedpreference byte[] value cleared after app killed via 'Force Stop' or task manager

Problem
I'm saving a byte[] in my shared preferences. I am able to close the app and reopen it with the value persisting in the Shared Preferences. When running the app and closing it via the 'Task Manager' or 'Force Close', the Shared Preference value for the byte[] is cleared. I don't understand this because other values persist fine.
This lead me to believe that this was due to some gson or Shared Preference issue with the byte[] so I converted it to a String and I still have the issue.
Edit:
I save the data during normal activity usage... after onCreate(), for example. It's not during onPuse() or onDestroy() I forgot to mention this. It would make sense if I did call it here and one or both of those weren't being called on the 'Force Close' scenario.
Shared Preference Code
Slightly modified to remove app specific implementation and data
private static final String SHARED_PREFERENCES_FILE_NAME = "SharedPreferenceName";
public static void setSharedPreferenceObjectBase64Encoded(Context context, String key, Object object) throws Exception {
// Need an editor to update shared preference values
SharedPreferences.Editor editor = context.getSharedPreferences(SHARED_PREFERENCES_FILE_NAME, Context.MODE_PRIVATE).edit();
Gson gson = new GsonBuilder().serializeNulls().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").create();
String encodedKey = Base64.encodeToString(key.getBytes(), 0, key.getBytes().length, Base64.DEFAULT);
String stringObject = gson.toJson(object);
String encodedObject = Base64.encodeToString(stringObject.getBytes(), 0, stringObject.getBytes().length, Base64.DEFAULT);
editor.putString(encodedKey, encodedObject);
editor.apply();
}
public static Object getSharedPreferenceObjectBase64Encoded(Context context, String key, Class<? extends Serializable> objectClass) throws Exception {
// Need an editor to update shared preference values
SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_FILE_NAME, Context.MODE_PRIVATE);
Gson gson = new GsonBuilder().serializeNulls().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").create();
String encodedKey = Base64.encodeToString(key.getBytes(), 0, key.getBytes().length, Base64.DEFAULT);
String encodedObject = prefs.getString(encodedKey, null);
if (encodedObject == null) {
throw new NullPointerException("encodedObject is null : No shared preference exists for key.");
}
String decodedObject = new String(Base64.decode(encodedObject, Base64.DEFAULT));
if(decodedObject == null){
throw new NullPointerException("decodedObject is null : Json decoding error.");
}
Object resultObject = gson.fromJson(decodedObject, objectClass);
if (resultObject == null) {
throw new NullPointerException("resultObject is null : Json decoding error.");
}
return resultObject;
}
`byte[]` Code
public static final String VALUE_KEY= "value.key";
public static void saveTheValue(Context context, byte[] encryptedPin) {
try {
USharedPreferenceManager.setSharedPreferenceObjectBase64Encoded(context, VALUE_KEY, encryptedPin);
} catch (Exception e) {
}
}
public static byte[] getTheValue(Context context) {
try {
return (byte[]) USharedPreferenceManager.getSharedPreferenceObjectBase64Encoded(context, VALUE_KEY, byte[].class);
} catch (Exception e) {
return null;
}
}
Any input would be greatly appreciated..
Sadly, I haven't been able to make any progress here. Any thoughts?
Update:
As per Super-califragilistic recommendation, I iterated through the key/value pairs in the SharedPreferences immediately before retrieving the value. I was Base64 encoding my key and value values; in order to read the key to ensure the value was in the SharedPreferences I had to use the keys in plain text. This solved the issue for me as the byte[] value was now being retrieved properly.
This seems strange to me but I can use it as a solution. I would still like to Base64 encode the keys, but it's not incredibly important.
Current Solution:
Removed the Base64 encoding of the SharedPreference Key for storage and retrieval and the value is now persisting in all cases.
This line of code String encodedObject = prefs.getString(encodedKey, null); means if the key does not exist it should return null, hence your key that you are checking does not exist.
To validate if your key/value exist use this code
for(Entry<String, ?> en : sharedPref.getAll().entrySet()){
en.getKey();//key
en.getValue();//value
}
you could stop that from happening override onPause() in the Activity or Fragment and call saveTheValue(Context context, byte[] encryptedPin) if you detect you need to save data or have already tried saving data eg.
private boolean forceSaveInOnPause= false;//global variable
//in your saving method
....//after you save
forceSaveInOnPause = true;
//in your onPause of Activity
if(forceSaveInOnPause){
//re-save
forceSaveInOnPause = false;
but since you already have a solution scratch all that :)
Try once with editor.commit() instead of apply(), see if that works
I think using Base64.NO_PADDING instead of Base64.DEFAULT both while reading and writing may solve the problem.

Sharing data between Android framework and app

Is it possible to share data between the Android framework and an app?
I'm modifying the Android framework login component so that a variable will be saved upon login, and then later retrieved by another app. I'm trying to use SharedPreferences, and my code looks like this:
In com.android.internal.policy.impl.PasswordUnlockScreen.java, I have the following code to write to the SharedPreferences.
SharedPreferences prefs = getContext().getSharedPreferences("mypref", Context.MODE_WORLD_READABLE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("mypref", "my data"
editor.commit();
In my other app, I have the following code to read from it.
Context con;
String data;
try {
con = this.createPackageContext("com.android.internal.policy.impl", 0);
SharedPreferences pref = con.getSharedPreferences("mypref", Context.MODE_PRIVATE);
data = pref.getString("mypref", "0")
} catch (NameNotFoundException e) {
data = "0";
Log.e("No data shared", e.toString());
}
When I run the code, I keep getting the NameNotFoundException, as it claims the application package com.android.internal.policy.impl is not found, so my data is always "0".
How can I share data between these 2 components?
Please take a look at Content Providers from the developer.android.com, this might help you. This is an example

Android: Retrieving shared preferences of other application

I have a settings application from which i have to retrieve other applications preferences, but i don't have the details of keys in them, how can i retrieve all the available keys and values in that preference?
Thanks,
Swathi
Okay! using this code in Application 1 ( with package name is "com.sharedpref1" ) to store data with Shared Preferences.
SharedPreferences prefs = getSharedPreferences("demopref",
Context.MODE_WORLD_READABLE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("demostring", strShareValue);
editor.commit();
And using this code in Application 2 to get data from Shared Preferences in Application 1. We can get it because we use MODE_WORLD_READABLE in application 1:
try {
con = createPackageContext("com.sharedpref1", 0);
SharedPreferences pref = con.getSharedPreferences(
"demopref", Context.MODE_PRIVATE);
String data = pref.getString("demostring", "No Value");
displaySharedValue.setText(data);
} catch (NameNotFoundException e) {
Log.e("Not data shared", e.toString());
}
More information please visit this URL:
http://androiddhamu.blogspot.in/2012/03/share-data-across-application-in.html
Assuming the preference are WORLD_READABLE, this might work:
final ArrayList<HashMap<String,String>> LIST = new ArrayList<HashMap<String,String>>();
// where com.example is the owning app containing the preferences
Context myContext = createPackageContext("com.example", Context.MODE_WORLD_WRITEABLE);
SharedPreferences testPrefs = myContext.getSharedPreferences("test_prefs", Context.MODE_WORLD_READABLE);
Map<String, ?> items = testPrefs .getAll();
for(String s : items.keySet()) {
// do something like String value = items.get(s).toString());
}
Additionally you have to add same android:sharedUserId in the both app's manifest file.
Unfortunately the docs now don't even explain MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE, instead saying:
This constant was depreciated in API level 17.
Creating world-readable files is very dangerous, and likely to cause security holes in applications. It is strongly discouraged; instead, ....etc
Since the depreciation, implementing file sharing between apps with sharedpreferences may be too risky, although it was simple. I'm not too concerned with security holes from the MODE_WORLD_READABLE mode in game apps where I just want to be able to transfer characters from one app to another. It's too bad they depreciated both sharing modes.
It can work if we want read perference value from other app/pkg/process.
but there is something wrong in jkhouw1's answer:
Context myContext = createPackageContext("com.example",
Context.MODE_WORLD_WRITEABLE);
It should be :
Context myContext = createPackageContext("com.example",
Context.CONTEXT_IGNORE_SECURITY);
though , CONTEXT_IGNORE_SECURITY and MODE_WORLD_WRITEABLE with the same value of "int 2"
At all ,thanks for this question and answers.
It's simple to retrieve store shared preferences data of one application to another application.
Step 1: add the same android:sharedUserId="android.uid.shared" in both app's manifest files.
Step 2: Store Value application1
SharedPreferences preferences = context.getSharedPreferences("token_id", Context.MODE_WORLD_READABLE);
Editor editor = preferences.edit();
editor.putString("shared_token", encryptedValue);
Log.e("aaa *** shared_token : ", encryptedValue.toString());
editor.commit();
Step 3: Get Value From application2
Context con = null;
try {
con = createPackageContext("application2 package name", Context.CONTEXT_IGNORE_SECURITY);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
try {
if (con != null) {
SharedPreferences pref = con.getSharedPreferences(
"token_id", Context.MODE_WORLD_READABLE);
String data = pref.getString("shared_token", "");
Log.d("msg", "Other App Data: " + data);
} else {
Log.d("msg", "Other App Data: Context null");
}
} catch (Exception e) {
e.printStackTrace();
}

How to store hashmap so that it can be retained it value after a device reboot?

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.)

Categories

Resources