Android methods for sharing data between different apk Methods of 2

  • 2021-01-02 21:59:27
  • OfStack

This article illustrates how Android can share data between different apk. To share for your reference, the details are as follows:

Android allocates a separate user space for each APK process, and userid in manifest corresponds to one Linux user (Android system is based on Linux).

Therefore, access to data between different APK(users) is prohibited by default.

But it also provides two forms of sharing data between APK:

1. Share Preference. / Content Provider

APK can specify the interface and data for any other APK to read. You need to implement the interface and Share data yourself. This article will not explain this in detail

2. Shared User id

With Shared User id, multiple APK with the same User id can be configured to run in the same process. Therefore, the default is to access arbitrary data from each other. It can also be configured to run in different processes, and can also access databases and files under other APK data directories. Just like accessing data 1 in this program.

For example, if a company has developed multiple Android programs, it can centralize data, images and other resources into APK A. Then all APK of this company use the same User ID, so all resources can be read from APK A.

Here's an example:

APK A and APK B are products of C, so if the user logs in successfully from APK A. Then there is no need to log in again when APK B is opened. The specific implementation is that A and B are set to the same User:

* Configure User ID on 2 APK AndroidManifest. xml:


<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.android.demo.a1"
android:sharedUserId="com.c">

This "com.c" is user id, and then packagename APK A is the above, APK B probably
Is "com android. demo. b1" the sky is the limit

With this setup, APK B can open the database in APK A as if it were a local database.

APK A stores login information under the A data directory. APK B reads the database under APK each time it is launched

Determine whether you have logged in:

Code in APK B:


friendContext = this.createPackageContext(
  "com.android.demo.a1",
  Context.CONTEXT_IGNORE_SECURITY);

packagecontext of A can be obtained by package of name

With this context, you can open the database directly

I hope this article has been helpful in Android programming.


Related articles: