Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Cannot access other app's SharedPreference

I've been searching for days to solve this, but with no success.

I want to get the shared preference settings from my old app and put it on to my new app. but I've encounter some security issue (suspect).

my code:

Context c = createPackageContext("my.app.pkg", Context.CONTEXT_IGNORE_SECURITY);            
SharedPreferences sp = c.getSharedPreferences("my.app.pkg", Context.CONTEXT_IGNORE_SECURITY);

Running the code above giving me this:

Attempt to read preferences file /data/data/my.app.pkg/shared_prefs/my.app.pkg_preferences.xml without permission

even thought the object sp is not null, but it does not retrieve anything from my old app.

I tried googling around and seems like most people can run the code above with no errors. Is there anything I've missed out?

like image 722
Desmond.Tan Avatar asked Oct 09 '22 08:10

Desmond.Tan


1 Answers

We've done exactly this for our Android book Android in Practice. The key is to use the same process and user ID for both apps. The sample code and sample apps are on Google Code (SharedProcessApp1 and SharedProcessApp2). You can go from there.

like image 171
Matthias Avatar answered Oct 12 '22 12:10

Matthias