In-depth analysis: Interaction between apps in Android (two, using componentname)

Source: Internet
Author: User

In the previous blog post on related topics, we learned how to use action to start activities outside of the current app to handle our business logic, and in this note I briefly introduce the use of componentname to interact with apps outside the current app.

Before introducing component, let us first understand the ComponentName class; ComponentName and intent are located under the Android.content package. We can see it from the Android official documentation. This class is primarily used to define an application component that is visible, such as: Activity. Service. Broadcastreceiver or ContentProvider.

So. How to define a component with componentname.

This is the ComponentName constructor: ComponentName (String pkg. String CLS)

We know that in an Android application it is assumed that a specific description of a component is required to know the application package name of the component, that is, the package= "XXX" under the manifest root node in the Androidmanifest.xml file. Xxxxx. XXXXX ". There is also the full pathname of the component in the application, which, for activity, is the value of the name attribute in the Activity node. So here we have a clear name for the application package name and component that can use ComponentName to encapsulate a component.

We already know that communication between components in Android often uses intent (Intent) to complete. Then there is a way to encapsulate a componentname in intent, and finally we are using the intent to complete the function we need to implement.

Below we use detailed code to describe how to use componentname to help us interact with other applications:

First we want to create two Android apps, Appsend and Appreceiver.

Appreceiver's Androidmainfest.xml


<?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= "one" android:targetsdkversion= "/> &lt" ; application android:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/A Pp_name "Android:theme=" @style/apptheme "> <activity <strong><span style=" COLOR: #ff 0000; " > 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> 

Appsend the start activity fragment in the:

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 you need to be able to download, click the Open link











In-depth analysis: Interaction between apps in Android (two, using componentname)

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.