Beginner Android Shareuserid (multi-app shared resources and services)

Source: Internet
Author: User

Each app in Android has a unique Linux user ID. With Shareduserid, we have two app apps with the same userid to see each other's files. To conserve resources, an APK with the same ID can also be used in the same Linux process. The role of Shareuserid: Data sharing, invoking other program resources.

Steps:

First, a new, A, a, two Android projects, a package name Com.rainwii.client B's package name Com.rainwii.service

Second, in A, B project to the manifest.xml to increase the same android:shareduserid= "Com.rainwii.share".

A manifest.xml

<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.rainwii.client"Android:versioncode= "1"Android:versionname= "1.0"android:shareduserid= "Com.rainwii.share" >    <USES-SDKandroid:minsdkversion= "8"android:targetsdkversion= "+" />    <ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" >        <ActivityAndroid:name= "Com.rainwii.client.MainActivity"Android:label= "@string/app_name" >            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN" />                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>        </Activity>    </Application></Manifest>

b manifest.xml

<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.rainwii.service"Android:versioncode= "1"Android:versionname= "1.0"    android:shareduserid= "Com.rainwii.share">    <USES-SDKandroid:minsdkversion= "8"android:targetsdkversion= "+" />    <ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" >        <ActivityAndroid:name= "Com.rainwii.service.MainActivity"Android:label= "@string/app_name" >            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN" />                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>        </Activity>    </Application></Manifest>

Third, the use of contextcreatepackagecontext in Mainactivity.class to obtain a context for another program.

A's Mainactivity.class

 Packagecom.rainwii.client;ImportCOM.RAINWII.MAIN.R;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.content.Context;Importandroid.content.pm.PackageManager.NameNotFoundException;ImportAndroid.util.Log;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {Button Startappbutton;    String str; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Try{Context ct= This. Createpackagecontext ("Com.rainwii.service", context.context_ignore_security); STR=ct.getstring (r.string.app_name); } Catch(namenotfoundexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Startappbutton=(Button) Findviewbyid (R.id.button1); Startappbutton.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubToast.maketext (mainactivity. This, ""+str, toast.length_short). Show ();                        }        }); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; }    }

B's Mainactivity.class

 PackageCom.rainwii.service;ImportCOM.RAINWII.MAIN.R;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.content.Context;Importandroid.content.pm.PackageManager.NameNotFoundException;ImportAndroid.util.Log;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {Button Startappbutton;    String str; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Try{Context ct= This. Createpackagecontext ("com.rainwii.client", context.context_ignore_security); STR=ct.getstring (r.string.app_name); } Catch(namenotfoundexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Startappbutton=(Button) Findviewbyid (R.id.button1); Startappbutton.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubToast.maketext (mainactivity. This, ""+str, toast.length_short). Show ();    }        }); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; }    }

Note: Thecontext has a createpackagecontext (Packagename,flags) method, which can be used to create another package, which is different from its own instance of the context, but the function is the same. This method has two parameters: 1, PackageName package name, to get the context of the package name

    2, Flags flag, there are Context_include_code and context_ignore_security two options. Context_include_code means including code, which means that the code inside the package can be executed. Context_ignore_security means ignoring security warnings, and if you do not add this flag, some features are not available and a security warning appears.

Beginner Android Shareuserid (multi-app shared resources and services)

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.