Android component communication intent-componnentname attribute

Source: Internet
Author: User

Componnentname attribute application instance

/Chapter06_intent_componentname/src/COM/Amaker/ch06/APP/mainactivity. Java

Code

  Package  Com. Amaker. ch06.app;

Import Android. App. activity;
Import Android. content. componentname;
Import Android. content. intent;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;

/**
* Test the componentname attribute of intent.
*/
Public Class Mainactivity Extends Activity {
Private Button BTN;
@ Override
Public Void Oncreate (bundle savedinstancestate ){
Super . Oncreate (savedinstancestate );
// Set view Layout
Setcontentview (R. layout. Main );
// Instantiate button
BTN = (Button) findviewbyid (R. Id. mybutton01 );
// Add click listener
BTN. setonclicklistener ( New Onclicklistener (){
@ Override
Public Void Onclick (view v ){
// Instantiate component name
Componentname CN = New Componentname (mainactivity. This , " Com. Amaker. ch06.app1. myactivity " );
// Instantiate intent
Intent intent = New Intent ();
// Set component name attributes for intent
Intent. setcomponent (CN );
// Start Activity
Startactivity (intent );
}
});
}
}

/Chapter06_intent_componentname/src/COM/Amaker/ch06/app1/myactivity. Java

Code

  Package  Com. Amaker. ch06.app1;

Import Android. App. activity;
Import Android. content. componentname;
Import Android. content. intent;
Import Android. OS. Bundle;
Import Android. widget. textview;

Import Com. Amaker. ch06.app. R;

/**
* Test the componentname attribute of intent.
*/
Public Class Myactivity Extends Activity {
// Declare textview
Private Textview TV;
@ Override
Public Void Oncreate (bundle savedinstancestate ){
// Set view Layout
Super . Oncreate (savedinstancestate );
Setcontentview (R. layout. my_layout );
// Get intent
Intent intent = This . Getintent ();
// Get component name object
Componentname CN = Intent. getcomponent ();
// Get package name
String packagename = CN. getpackagename ();
// Obtain the class name
String classname = CN. getclassname ();
// Instantiate textview
TV = (Textview) findviewbyid (R. Id. textview01 );
// Display
TV. settext ( " Package Name: " + Packagename + " \ N " + " Component class name: " + Classname );
}
}

/Chapter06_intent_componentname/RES/layout/Main. xml

Code

  <?  XML version = "1.0" encoding = "UTF-8"  ?>  
< Linearlayout Xmlns: Android = "Http://schemas.android.com/apk/res/android"
Android: Orientation
= "Vertical"
Android: layout_width
= "Fill_parent"
Android: layout_height
= "Fill_parent"
>
< Button
Android: Text = "Test the intent component name attribute"
Android: ID
= "@ + ID/mybutton01"
Android: layout_width
= "Wrap_content"
Android: layout_height
= "Wrap_content" > </ Button >
</ Linearlayout >

/Chapter06_intent_componentname/RES/layout/my_layout.xml

Code

  <?  XML version = "1.0" encoding = "UTF-8"  ?>  
< Linearlayout Xmlns: Android = "Http://schemas.android.com/apk/res/android"
Android: Orientation
= "Vertical"
Android: layout_width
= "Fill_parent"
Android: layout_height
= "Fill_parent"
>

< Textview
Android: Text = "@ + ID/textview01"
Android: ID
= "@ + ID/textview01"
Android: layout_width
= "Wrap_content"
Android: layout_height
= "Wrap_content" > </ Textview >

</ Linearlayout >

 

 

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.