In-depth analysis: Interaction Between apps in Android (2. Using ComponentName)

Source: Internet
Author: User

In the previous blog on the relevant topic, we learned how to use Action to start activities outside of the current application to process our business logic, in this document, I will briefly introduce how to use ComponentName to interact with applications other than the current application.

Before introducing Component, we should first understand the ComponentName class. ComponentName and Intent are located in android. under the content package, we can see from the official Android documentation that this class is mainly used to define an application component, such as Activity, Service, BroadcastReceiver, or ContentProvider.

Then, how can we use ComponentName to define a component.

This is the ComponentName constructor: ComponentName (String pkg, String cls)

We know that in Android applications, If you want to describe a component in detail, you need to know the package name of the component, that is, the package name in AndroidManifest. package = "XXX. XXXXX. XXXXX ", and the complete path name of the component in the application. Take Activity as an example, that is, the value of the name attribute in the activity node. So here we understand that you can use ComponentName to encapsulate the application package name and component name of a component.

We already know that the communication between components in Android is usually completed with Intent (Intent), so there is a method in Intent that can encapsulate a ComponentName, finally, we are using the intent to complete the functions we need to implement.

The following code describes how to use ComponentName to help us interact with other applications:

First, we need to create two Android applications: Begin end and appreceiver.

AndroidMainfest. xml of appreceiver


<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"   <span style="color:#cc0000;"> <strong>package="com.example.appreceiver"</strong></span>    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="11"        android:targetSdkVersion="18" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity           <strong><span style="color:#ff0000;"> android:name="com.example.appreceiver.MainActivity"</span></strong>            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

The starting Activity segment in the end:

public void button(View view) {<strong><span style="color:#ff0000;">ComponentName cn=new ComponentName("com.example.appreceiver", "com.example.appreceiver.MainActivity");</span></strong>Intent intent = new Intent();<strong><span style="color:#ff0000;">intent.setComponent(cn);</span></strong>startActivityForResult(intent, 2);}

The complete case has been packaged and uploaded to csdn. If necessary, you can download it and click the open link.











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.