[Android] multiple Android APK data sharing

Source: Internet
Author: User

Android assigns a separate user space to each APK process. The userid in its manifest corresponds to a Linux User (the Android system is based on Linux. therefore, mutual access to data between different APK (users) is disabled by default.

However, it also provides two types of APK data sharing:

 1. Share Preference./Content Provider
APK can specify interfaces and data to read from any other APK. You need to implement interfaces and Share data on your own.
  
 2. Shared User id
With the Shared User id, multiple APK with the same User id can be configured to run in the same process. by default, the system can access any data from each other. you can also configure to run different processes and access the databases and files in the data directory of other APK files. just like accessing the data of this program.

For example, if A company develops multiple Android programs, you can put data, images, and other resources in apk. then all the company's APK uses the same User ID, so all resources can be read from apk.

 For example:
Both apk a and apk B are products of Company C. If the user logs in from APK A successfully. you do not need to log in again when you open apk B. the specific implementation is to set A and B to the same User ID:

* Configure the User ID in AndroidManifest. xml of two APK files:
Package = "eoe. android. demo. a1"
Android: sharedUserId = "com. c">
This "com. c" is the user id, and packagename apk a is the above content. apk B may
Is "com. android. demo. b1 ".
After this setting, apk B can open the database in APK A just like opening the local database.

Apk a stores the login information in the data directory of A. apk B reads the database under APK A each time it starts to determine whether it has been logged on:
Code in apk B:
FriendContext = this. createPackageContext (
"Com. android. demo. a1 ",
Context. CONTEXT_IGNORE_SECURITY );

You can use package name of A to obtain packagecontext of.
With this context, you can directly open the database.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.