Android Essay--Inter-process communication (a) Activity chapter

Source: Internet
Author: User

In the development of Android application, we will encounter the situation of cross-process communication, for example: When you call the QQ Address Book calls the system dial-up application, some news clients can share news to QQ, and other applications, these are cross-process communication situation. In short, an application invokes another application and passes some data. In Android, there are activity, Content Provider, Broadcastreceiver, service four components, the clever is the Android process communication is inseparable from the four components (also said that the aidl mechanism, But now there is no difference between service and Aidl (/-\), the core mechanism of communication between components is intent, through intent can open an activity or service.

For these four components, the most common is also the most easy to understand is the activity, because the activity is visible and touch, so the cross-process communication is first from the activity first.

Activity can be accessed either within the same process or across processes, and whether the main difference between access across processes is that intent is showing intent or implicit intent.

Intent Type:

1. Display intent--directly specifies the activity class to start, typically for communication between different components within the same process, in the form of new Intent (This,target.class);

// display intent, typically used to initiate activity in the same process New Intent (This, otheractivity.  Class); startactivity (intent);

2. Implicit intent--is generally used for cross-process communication and is registered in the Androidmanifest.xml file in the form of intent.setaction (String action).

// Implicit Intent New Intent (); // set the ACTION to start the system dial-up interface, Action_call is actually the string constant "Android.intent.action.CALL" intent.setaction (Intent.action_call); startactivity (intent);

Now that we know that the inter-process communication between activity depends largely on implicit intent, how do we implement implicit intent? The answer is, as stated above, registering in the Androidmanifest.xml file is OK. The following is the direct link to the code, code comments will be interspersed with some attention to the content.

Create a otheractivity Android project and change the layout file slightly:

<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context=". Mainactivity " >    <!--It is not recommended to write content directly in the text in actual development. -    <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "I was summoned by another activity across the process." /></Relativelayout>

Next, modify the Androidmanifest.xml file for the Otheractivity project:

<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.lsj.otheractivity"Android:versioncode= "1"Android:versionname= "1.0" >    <USES-SDKandroid:minsdkversion= "8"android:targetsdkversion= "+" />    <ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" >        <!--One <activity> includes: 0 or more <intent-filter> -        <!--It is mainly used as a matching criterion, and can be matched successfully by the <action>, <category>, <data> three tags jointly determined.  -        <ActivityAndroid:name= "Com.lsj.otheractivity.MainActivity"Android:label= "@string/app_name" >            <!--One <intent-filter> includes: one or more <action> 0 or more <category> 0 or one <data> -            <Intent-filter>                <!--<action> is the key to implementing an implicit intent activation activity in another process -                <!--to start this activity, simply add the string constants in the <action> tag to the Setaction -                <ActionAndroid:name= "Com.lsj.otherActivity" />                <!--If you have a custom action, remember to add the following sentence, or you will get an error -                <categoryAndroid:name= "Android.intent.category.DEFAULT" />                <ActionAndroid:name= "Android.intent.action.MAIN" />                <!--<category> is used to specify the classification characteristics of the activity, which represents this activity as the start-up interface for this program -                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />                <!--<data> tags are not used, temporarily ignored -            </Intent-filter>        </Activity>    </Application></Manifest>

Next, in creating an Activity Android project, add the following code to its mainactivity:

// Create a new intent object New Intent (); // Sets the action,action of the intent object to the Action value intent.setaction ("com.lsj.otherActivity") registered in the otheractivity configuration file ; // Activate activitystartactivity (intent);

Install the otheractivity first, then install and run the activity, and you will find that Otheractivity has been opened. This realizes the inter-process communication between the activity, and for transmitting data, it can be used bundle or intent. Putextras () to achieve.

For the previous setting, action might have this confusion, what if a intent corresponds to multiple matching results? The system will be treated differently depending on the component, and if it is a service then the service can start and process the message, and if it is activity, a dialog box will pop up to let the user make a selection.

Reference: Android multi-process communication

Author: Sky Road

Reprint please indicate source: http://www.cnblogs.com/travellife/

Android Essay--Communication across processes (i) Activity 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.