I need to access the Shared Preference file of another application. ( The device is rooted! ) Currently I seem to be facing a permission issues. Here is the current code that I run:
Context con = createPackageContext("com.sam.sample", CONTEXT_IGNORE_SECURITY);
SharedPreferences sharedPref = con.getSharedPreferences("namefile", MODE_PRIVATE);
Map data = sharedPref.getString("name", "");
Log.d("Name", "name:" + name);
When I run this code I get the following error:
Attempt to read preferences file /data/data/com.sam.sample/shared_prefs/namefile.xml without permission
I have also tried to run su
command but I dont want the user to be notified that I want to get read access with chmod 775
on the XML file.
Any help is appreciated.
To do this when creating SharedPreference in "com.sam.sample" it should be created using Context.MODE_WORLD_READABLE mode. So if you have code of "com.sam.sample" application, it should look something like below
SharedPreferences prefs = getSharedPreferences("namefile",
Context.MODE_WORLD_READABLE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("name", strShareValue);
editor.commit();
Also note that changing the mode to Context.MODE_WORLD_READABLE is not recommended as it may lead to security holes. As of NOS this will throw SecurityException. More info:-
https://developer.android.com/reference/android/content/Context.html#MODE_WORLD_READABLE
Ideally ContentProvider should be used.
For example query function of your ContentProvider can be
MatrixCursor cursor = new MatrixCursor(new String[] { "name" }, 1); cursor.addRow(new Object[] { readSharedPreference() }); return cursor;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.