• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

read a value from an XML file stored in shared_prefs under another app

Poldenais

Lurker
HI all, My Android app {app2} needs to read a value from an XML file stored in shared_prefs under another app{app1} [API 24].

In my app I put in the following Code to try and read the shared preferences file that is stored under app1 but its not working:

String key = "";

try {
con = createPackageContext("com.app1", Context.CONTEXT_IGNORE_SECURITY);


SharedPreferences prefs = con.getSharedPreferences("app1_conf", MODE_PRIVATE);

key = prefs.getString("ITEM1", "Couldn't Find");
Log.i("mainActivity", "My item is " + key);

} catch(PackageManager.NameNotFoundException e){
Log.e("Not Data Shared", e.toString());
}



 
You could use MODE_WORLD_READABLE but it's deprecated now.
https://chrisrisner.com/Accessing-the-Shared-Preferences-of-a-Different-Application-in-Android
Code:
/**
* File creation mode: allow all other applications to have read access to
* the created file.
* <p>
* Starting from {@link android.os.Build.VERSION_CODES#N}, attempting to use this
* mode throws a {@link SecurityException}.
*
* @deprecated Creating world-readable files is very dangerous, and likely
*             to cause security holes in applications. It is strongly
*             discouraged; instead, applications should use more formal
*             mechanism for interactions such as {@link ContentProvider},
*             {@link BroadcastReceiver}, and {@link android.app.Service}.
*             There are no guarantees that this access mode will remain on
*             a file, such as when it goes through a backup and restore.
* @see android.support.v4.content.FileProvider
* @see Intent#FLAG_GRANT_WRITE_URI_PERMISSION
*/
@Deprecated
public static final int MODE_WORLD_READABLE = 0x0001;
You can't read and change shared preferences outside the application.
Android recommends to use BroadcastReceivers, Service and ContentProviders to get access to application data from outside.
 
Last edited:
Back
Top Bottom