I. Basic Knowledge:
1. An Intent object contains a set of information:
1. Component name specifies the Activity to be started
2. What to do with Action
3. Data Transmission
4. Category
5. Extras key-value pairs
6. Flags
2. Basic Intent usage:
[Java] view plaincopyprint? // Generate an Intent object
Intent intent = new Intent ();
Intent. putExtra ("testIntent", "123"); // pass data
Intent. setClass (Activity_02.this, OtherActivity. class); // specifies which Activity to jump to (the second parameter)
// Activity_02.this.startActivity (intent );
StartActivity (intent );
// Generate an Intent object
Intent intent = new Intent ();
Intent. putExtra ("testIntent", "123"); // pass data
Intent. setClass (Activity_02.this, OtherActivity. class); // specifies which Activity to jump to (the second parameter)
// Activity_02.this.startActivity (intent );
StartActivity (intent );
[Java]
// Receives data from Intent.
Intent intent = getIntent ();
String value = intent. getStringExtra ("testIntent"); // receives Intent data
MyTextView = (TextView) findViewById (R. id. myTextView );
// MyTextView. setText (R. string. other );
MyTextView. setText (value );
// Receives data from Intent.
Intent intent = getIntent ();
String value = intent. getStringExtra ("testIntent"); // receives Intent data
MyTextView = (TextView) findViewById (R. id. myTextView );
// MyTextView. setText (R. string. other );
MyTextView. setText (value );
3. Button event registration:
[Java]
Private Button myButton = null;
MyButton = (Button) findViewById (R. id. myButton );
MyButton. setOnClickListener (new MyButtonListener ());
Class MyButtonListener implements OnClickListener {
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
// Generate an Intent object
Intent intent = new Intent ();
Intent. putExtra ("testIntent", "123"); // pass data
Intent. setClass (Activity_02.this, OtherActivity. class); // specifies which Activity to jump to (second parameter
Number)
// Activity_02.this.startActivity (intent );
StartActivity (intent );
/*
Uri uri = Uri. parse ("smsto: 0800000123 ");
Intent intent = new Intent (Intent. ACTION_SENDTO, uri );
Intent. putExtra ("sms_body", "The SMS text ");
StartActivity (intent );
*/
}
}
Private Button myButton = null;
MyButton = (Button) findViewById (R. id. myButton );
MyButton. setOnClickListener (new MyButtonListener ());
Class MyButtonListener implements OnClickListener {
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
// Generate an Intent object
Intent intent = new Intent ();
Intent. putExtra ("testIntent", "123"); // pass data
Intent. setClass (Activity_02.this, OtherActivity. class); // specifies which Activity to jump to (second parameter
Number)
// Activity_02.this.startActivity (intent );
StartActivity (intent );
/*
Uri uri = Uri. parse ("smsto: 0800000123 ");
Intent intent = new Intent (Intent. ACTION_SENDTO, uri );
Intent. putExtra ("sms_body", "The SMS text ");
StartActivity (intent );
*/
}
}
Ii. Code display:
1. "Activity_02srcyanactivity_02Activity_02.java"
[Java]
Package yan. activity_02;
Import android.net. Uri;
Import android. OS. Bundle;
Import android. app. Activity;
Import android. content. Intent;
Import android. view. Menu;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Public class Activity_02 extends Activity {
Private Button myButton = null;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_02 );
MyButton = (Button) findViewById (R. id. myButton );
MyButton. setOnClickListener (new MyButtonListener ());
}
Class MyButtonListener implements OnClickListener {
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
// Generate an Intent object
Intent intent = new Intent ();
Intent. putExtra ("testIntent", "123 ");
Intent. setClass (Activity_02.this, OtherActivity. class );
// Activity_02.this.startActivity (intent );
StartActivity (intent );
/*
Uri uri = Uri. parse ("smsto: 0800000123 ");
Intent intent = new Intent (Intent. ACTION_SENDTO, uri );
Intent. putExtra ("sms_body", "The SMS text ");
StartActivity (intent );
*/
}
}
}
Package yan. activity_02;
Import android.net. Uri;
Import android. OS. Bundle;
Import android. app. Activity;
Import android. content. Intent;
Import android. view. Menu;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Public class Activity_02 extends Activity {
Private Button myButton = null;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_02 );
MyButton = (Button) findViewById (R. id. myButton );
MyButton. setOnClickListener (new MyButtonListener ());
}
Class MyButtonListener implements OnClickListener {
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
// Generate an Intent object
Intent intent = new Intent ();
Intent. putExtra ("testIntent", "123 ");
Intent. setClass (Activity_02.this, OtherActivity. class );
// Activity_02.this.startActivity (intent );
StartActivity (intent );
/*
Uri uri = Uri. parse ("smsto: 0800000123 ");
Intent intent = new Intent (Intent. ACTION_SENDTO, uri );
Intent. putExtra ("sms_body", "The SMS text ");
StartActivity (intent );
*/
}
}
}
2. "Activity_02srcyanactivity_02OtherActivity.java"
[Java]
Package yan. activity_02;
Import android. app. Activity;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. widget. TextView;
Public class OtherActivity extends Activity {
Private TextView myTextView = null;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
// TODO Auto-generated method stub
Super. onCreate (savedInstanceState );
SetContentView (R. layout. other );
Intent intent = getIntent ();
String value = intent. getStringExtra ("testIntent ");
MyTextView = (TextView) findViewById (R. id. myTextView );
// MyTextView. setText (R. string. other );
MyTextView. setText (value );
}
}
Package yan. activity_02;
Import android. app. Activity;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. widget. TextView;
Public class OtherActivity extends Activity {
Private TextView myTextView = null;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
// TODO Auto-generated method stub
Super. onCreate (savedInstanceState );
SetContentView (R. layout. other );
Intent intent = getIntent ();
String value = intent. getStringExtra ("testIntent ");
MyTextView = (TextView) findViewById (R. id. myTextView );
// MyTextView. setText (R. string. other );
MyTextView. setText (value );
}
}
3. "Activity_02reslayoutactivity_02.xml"
[Java]
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
Android: id = "@ + id/myButton"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
Android: id = "@ + id/myButton"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>
4. "Activity_02reslayoutother.xml"
[Java]
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
Android: id = "@ + id/myTextView"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
Android: id = "@ + id/myTextView"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>
5. "Activity_02resvaluesstrings.xml"
[Java]
Activity_02
Hello world!
Settings
Other string
Activity_02
Hello world!
Settings
Other string
6. "Activity_02AndroidManifest.xml"
[Java] view plaincopyprint?
Package = "yan. activity_02"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
Android: minSdkVersion = "4"
Android: targetSdkVersion = "4"/>
Android: allowBackup = "true"
Android: icon = "@ drawable/ic_launcher"
Android: label = "@ string/app_name"
Android: theme = "@ style/AppTheme">
Android: name = "yan. activity_02.Activity_02"
Android: label = "@ string/app_name">
Android: label = "@ string/other">
Package = "yan. activity_02"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
Android: minSdkVersion = "4"
Android: targetSdkVersion = "4"/>
Android: allowBackup = "true"
Android: icon = "@ drawable/ic_launcher"
Android: label = "@ string/app_name"
Android: theme = "@ style/AppTheme">
Android: name = "yan. activity_02.Activity_02"
Android: label = "@ string/app_name">
Android: label = "@ string/other">
Note the activity declaration in this file:
Android: label = "@ string/other">
Iii. effect display:
Click the Button above --> to jump to another Activity.