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 >