ProgramFile
/Chapter06_intent_category/src/COM/Amaker/test/mainactivity. Java
Code
Package com. Amaker. test;
Import Android. App. activity;
Import Android. content. intent;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;
/**
* Test the category attribute of intent.
*/
Public class mainactivity extends Activity {
// Declare the button
Private button B1;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
// Set the current layout
Setcontentview (R. layout. Main );
// Instantiate the button
B1 = (button) findviewbyid (R. Id. button01 );
// Add a listener for the button
B1.setonclicklistener (New onclicklistener (){
@ Override
Public void onclick (view v ){
// Instantiate intent
Intent I = new intent ();
// Add the action attribute
I. setaction (intent. action_main );
// Add the category attribute
I. addcategory (intent. category_home );
// Start the activity
Startactivity (I );
}
});
}
}
Layout File
/Chapter06_intent_category/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" >
< Textview
Android: layout_width = "Fill_parent"
Android: layout_height = "Wrap_content"
Android: Text = "Intent category test" />
< Button
Android: ID = "@ + ID/button01"
Android: layout_width = "Wrap_content"
Android: layout_height = "Wrap_content"
Android: Text = "Back to home" > </ Button >
</ Linearlayout >