Android: how to remove general settings in another package

In my Android application, I encoded to read the general data of another Android application, and then remove that data from the general settings. My code is as follows:

try { con = createPackageContext("com.testapp.ws", 0); SharedPreferences pref = con.getSharedPreferences("demopref", Context.MODE_PRIVATE); ipAdr = pref.getString("demostring", "No Value"); pref.edit().remove("demopref").commit(); } 

This shows the following error:

 06-12 11:52:07.400: E/ApplicationContext(3587): Couldn't rename file /data/data/com.testapp.ws/shared_prefs/demopref.xml to backup file /data/data/com.testapp.ws/shared_prefs/demopref.xml.bak 

I used this method in my other application to create shared data

  public void shareData(){ String strShareValue = ip; SharedPreferences prefs = getSharedPreferences("demopref",Context.MODE_WORLD_READABLE); SharedPreferences.Editor editor = prefs.edit(); editor.putString("demostring", strShareValue); editor.commit(); } 

How can i do this? Can I add a manifest file?

Thanks!

+6
source share
4 answers

If you use android: sharedUserId in manifest files, it should work. This is a permissions issue that I used for myself.

To do this, you just need to add the android:sharedUserId="com.example.you" tag to the <manifest> in your AndroidManifest.xml file for both of your applications (and com.example.you should be the same in both applications, of course).

Example of launching a manifest file:

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="package.name" android:versionCode="1" android:versionName="1.0" android:sharedUserId="com.example.you" > ... 

For a detailed description of how to get this working, see my answer to How can I share a SharedPreferences file in two different Android applications?

+4
source

You need to use MODE_WORLD_READABLE instead of MODE_PRIVATE . Read the docs for more details.

Here's a tutorial to further check if you have more errors.

+1
source

While other solutions here will technically work in most cases, the infrastructure that Android has provided for you to exchange data between processes / applications is ContentProvider . This may seem like a lot of extra abstraction, but it is one that is guaranteed to work.

While the interface for this component reflects the challenges in the database, the basic data structure can be anything you like. In particular, you can return MatrixCursor in response to requests that provide the contents of your SharedPreferences object, and you can implement a URI scheme for delete / update calls that can be used to change preferences from other applications.

Here is a link to a blog post from another developer who used MatrixCursor to exchange preferences.

NTN

+1
source

I struggled with SharedPreferences the last three days, but I think I finally decided it for myself. Here are some tips and fixes you should try that might help you (and the loads of other stackoverflow users).

Please note that all my observations relate to ICS (4.0.2 and 4.0.4).

  • If you change android:sharedUserId , delete the application , because the rights to the file / folder will be incorrect. Therefore, you should not change this value if you have already changed the application.
  • If you change the value of Context.MODE_* , delete the application data (or the entire application) to ensure that the file permissions are not incorrect.
  • If you are using android:sharedUserId , be sure to sign applications with the same certificate .
  • Use a different file name for settings in two applications. I had problems with an application just looking at local general settings, although I used createPackageContex() .

This is what worked for me at the end:

  • I used the same android:sharedUserId for two applications. (Not the same android:process .)
  • I used Context.MODE_WORLD_READABLE to read and write local prefs and to read other application prefixes.
  • I used Context.CONTEXT_IGNORE_SECURITY when calling createPackageContext() .
+1
source

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


All Articles