Sharing data between Android framework and app - android

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

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!

Read preference of other application 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.

accessing sharedpreference across applications

I am using getDefaultSharedPreferences(con) in my application to store preferences. Now I want to access this shared preference in another application.
I used following method:
con = this.createPackageContext("com.example.preferences", Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
SharedPreferences filePref = PreferenceManager.getDefaultSharedPreferences(con);
if(filePref != null){
Toast.makeText(getApplicationContext(), "file pref not null --- ", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(), "file pref is null **** ", Toast.LENGTH_LONG).show();
}
Map<String,?> allkeys = filePref.getAll();
Toast.makeText(getApplicationContext(), "file pref size **** "+allkeys.size(), Toast.LENGTH_LONG).show();
for(Map.Entry<String,?> entry : allkeys.entrySet()){
Toast.makeText(getApplicationContext(), "~~~ file pref --- map values --- "+entry.getKey() + ": "+entry.getValue().toString(), Toast.LENGTH_LONG).show();
}
Another way which tried is specifying file name & accessing it, as follow;
SharedPreferences filePref = gvcon.getSharedPreferences("com.example.preferences_preferences", Context.MODE_PRIVATE);
With this method I am able to access SharedPrefernce file, it returns file is not null but when I check for file size it shows 0. I am not able to read preference value from file.
I used shareduserid of another application so that I get full access to that application.
What is the proper way to go about this?
I'm not sure that I understood your problem completely, but some times this implementation will help you to access data across packages.
Use getSharedPreferences() to store data in shared preferences
This method will store data in shared preferences
public void dataWriter(){
String strShareValue = "Hello! this is shared data";
SharedPreferences prefs = getSharedPreferences("demopref",Context.MODE_WORLD_READABLE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("demostring", strShareValue);
editor.commit();
}
You can access that shared data from another package using this code sample
public void dataRead(){
Context con;
try {
con = createPackageContext("PACKAGE NAME THAT SHARES DATA", 0);
SharedPreferences pref = con.getSharedPreferences("demopref", Context.MODE_PRIVATE);
String dataShared = pref.getString("demostring", "No Value");
}
catch (NameNotFoundException e) {
Log.e("Not data shared", e.toString());
}
}
}

How to use SharedPreferences to store persistant data Android

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();

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();
}

Categories

Resources