Android: cannot access another SharedPreference application

I was looking for days to solve this problem, but without success.

I want to get general preference settings from my old application and put it in a new application. but I ran into a security problem (suspect).

my code is:

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

Executing the above code gives me the following:

Try to read the settings file /data/data/my.app.pkg/shared_prefs/my.app.pkg_preferences.xml without permission

I even thought that the sp object is not null, but it does not extract anything from my old application.

I tried searching the site and it looks like most people can run the code above without error. Did I miss something?

+4
source share
3 answers

We did just that for our Android Android book in practice . The key must use the same process and user ID for both applications. Sample code and sample applications are on Google Code (SharedProcessApp1 and SharedProcessApp2). You can go from there.

+3
source

You can really share preferences between applications. That is why it was called SharedPreferences.

What you need to do is make sure that both applications are signed with the same certificate, and both of them share the same SharedUserId in the AndroidManifest.xml file: read here .

This is due to the fact that you get SharedPreferences from

 PreferenceManager.getDefaultSharedPreferences(context) 

always MODE_PRIVATE.

However, in the application, you can also get the SharedPreferences object in context with the following:

 SharedPreferences prefs = getSharedPreferences("my_public_shared_prefs", MODE_WORLD_READABLE); 

which you can freely backtrack from another application with the following:

 Context context = createPackageContext("my_target_app_package", Context.CONTEXT_IGNORE_SECURITY); SharedPreferences prefs = context.getSharedPreferences("my_public_shared_prefs", MODE_WORLD_READABLE); 

Make sure that you do not store any personal information there, because this WORLD is read, which means that you and anyone else can read this data.

To finish, if you want to get the SharedPreferences of your old application, you will need to update the old application using the SharedUserId in the manifest file.

+1
source

Android applications run in their own sandbox, so one application cannot access data from another activity. BUt If an application wants to share some of its data, this can be achieved using ContentProvider.

See content provider: http://developer.android.com/guide/topics/providers/content-providers.html

-1
source

Source: https://habr.com/ru/post/1402836/


All Articles