Android multiple APK data sharing

Source: Internet
Author: User

Android assigns a separate user space to each APK process, and the userid in its manifest corresponds to a Linux user (the Android system is based on Linux). Therefore, it is forbidden to access data between different apk (users) by default.

But it also provides the form of shared data between 2 different apk:

1. Share preference. /Content Provider

APK can specify interfaces and data to be read by any other apk. You need to implement the interface and share data yourself.

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. So the default is that you can access arbitrary data to each other. It can also be configured to run as a different process, with access to databases and files in other APK data directories. Just like accessing the data in this program.

For example, a company developed a number of Android programs, you can put data, pictures and other resources to focus on apk a. All apk of the company then use the same user ID, and all resources can be read from apk a.

As an example:

APK A and APK B are all C's products, so if a user logs in from APK A, then it's not necessary to log on again when you open APK B. The realization is that A and B are set to the same user ID:

* Configure the user ID in the 2 apk androidmanifest.xml:

Package= "Eoe.android.demo.a1"

Android:shareduserid= "COM.C" >

This "COM.C" is the user ID, and then PackageName APK A is the above content, APK B May

It's "com.android.demo.b1," there's no limit.

Once this is set, APK B can open the database in APK a like the local database.

APK A puts the login information under the Data directory of a. APK b reads the database under APK A at each startup to determine whether it has logged in:

Code in APK B:

Friendcontext = This.createpackagecontext (

"Com.android.demo.a1",

context.context_ignore_security);

By package name of a, you get a packagecontext

With this context, you can open the database directly.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/

Related Article

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.