Learn android <Intent preliminary. 40 ..> and androidintent from scratch
Intent is a very important part of android, which is widely used in Activity and service.
1 |
Public void startActivity (Intent intent) |
Normal |
Start an Activity and transmit data through Intent |
2 |
Public void startActivityForResult (Intent intent, int requestCode) |
Normal |
Start and receive data from another Activity program. onActivityResult () can be triggered only when requestCode is greater than 0 () |
3 |
Public Intent getIntent () |
Normal |
Returns the Intent of the current Activity. |
4 |
Protected void onActivityResult (int requestCode, int resultCode, Intent data) |
Normal |
This method is used to process the return operation when Intent data is returned. |
5 |
Public void finish () |
Normal |
When this method is called, the previous Activity program is returned and the onActivityResult () method is automatically called. |
6 |
Public final Cursor managedQuery (Uri uri, String [] projection, String selection, String [] selectionArgs, String sortOrder) |
Normal |
Processing the returned Cursor result set |
Next I will explain it step by step using examples
The simplest Intent
Main Interface xml
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: paddingBottom = "@ dimen/activity_vertical_margin" android: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/plugin" tools: context = ". mainActivity "> <TextView android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: text =" @ string/hello_world "/> <Button android: id = "@ + id/button1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignParentBottom = "true" android: layout_centerHorizontal = "true: layout_marginBottom = "141dp" android: text = ""/> </RelativeLayout>
Jump interface Xml
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: paddingBottom = "@ dimen/activity_vertical_margin" android: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/plugin" tools: context = ". mainActivity "> <TextView android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: text =" I Am a second screen "/> </RelativeLayout>
Package com. example. intent1; import android. OS. bundle; import android. app. activity; import android. content. intent; import android. view. menu; import android. view. view; import android. widget. button; public class MainActivity extends Activity {private Button button; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); button = (Button) this. findViewById (R. id. button1); button. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubIntent intent = new Intent (MainActivity. this, Other. class); // create the Intent object startActivity (intent); // enable intentMainActivity. this. finish (); // end the current Activity }});}}
Otner. java
package com.example.intent1;import android.app.Activity;import android.os.Bundle;import android.text.style.SuperscriptSpan;public class Other extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.other);}}
Next, let's take a look at the Intent that can pass data.
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: paddingBottom = "@ dimen/activity_vertical_margin" android: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/plugin" tools: context = ". mainActivity "> <Button android: id =" @ + id/button1 "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: layout_alignParentBottom =" true "android: layout_centerHorizontal = "true" android: text = "pass"/> <EditText android: id = "@ + id/edit" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignParentTop = "true" android: layout_centerHorizontal = "true" android: EMS = "10" android: text = "/> </RelativeLayout> <RelativeLayout xmlns: android =" http://schemas.android.com/apk/res/android "xmlns: tools =" http://schemas.android.com/tools "android: layout_width =" match_parent "android: layout_height = "match_parent" android: paddingBottom = "@ dimen/activity_vertical_margin" android: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/activity_vertical_margin" tools: context = ". mainActivity "> <Button android: id =" @ + id/button1 "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: layout_alignParentBottom =" true "android: layout_centerHorizontal = "true" android: text = "pass"/> <EditText android: id = "@ + id/edit" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignParentTop = "true" android: layout_centerHorizontal = "true" android: EMS = "10" android: text = ""/> </RelativeLayout>
Other. xml
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <TextView android: id = "@ + id/textView1" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "TextView"/> <Button android: id = "@ + id/button2" android: gravity = "center_horizontal" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Get Data"/> </LinearLayout>
Java files
Package com. example. intent2; import android. OS. bundle; import android. r. integer; import android. app. activity; import android. content. intent; import android. view. menu; import android. view. view; import android. widget. button; import android. widget. editText; public class MainActivity extends Activity {private Button button; private EditText info; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); button = (Button) this. findViewById (R. id. button1); info = (EditText) this. findViewById (R. id. edit); button. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubIntent intent = new Intent (MainActivity. this, Other. class); // sets intent. putExtra ("info", info. getText (). toString (); // Add the Additional Information startActivity (intent) for the intent; // start the intent MainActivity. this. finish (); // terminate the current Ativity }});}}
Other. java
Package com. example. intent2; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. widget. button; import android. widget. textView; public class Other extends Activity {Intent intent; private Button button; private TextView text; @ Overrideprotected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stubsuper. onCreate (savedInstanceState); super. setContentView (R. layout. other); button = (Button) this. findViewById (R. id. button2); text = (TextView) this. findViewById (R. id. textView1); intent = super. getIntent (); // get the intent object button. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubString info = intent. getStringExtra ("info"); // get the transmitted data text. setText (info); // display data }});}}
Use Intent to pass and return data
Using Intent to test adult height and weight
Body Mass Index = body mass (kg) in addition to the height (meters) of the square kg/m2 normal weight: Body Mass Index = 18-25 overweight: Body Mass Index = 25-30 Mild Obesity: body mass index> 30 moderate obesity: Body mass index> 35 severe obesity: Body mass index> 40
Xml
Package com. example. inten3; import android. OS. bundle; import android. app. activity; import android. content. intent; import android. view. menu; import android. view. view; import android. widget. button; import android. widget. editText; import android. widget. textView; public class MainActivity extends Activity {private Button resetInfo, getInfo; private EditText height, weight; private TextView detail; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); super. setContentView (R. layout. activity_main); resetInfo = (Button) this. findViewById (R. id. button1); // reset the information Button getInfo = (Button) this. findViewById (R. id. button2); // obtain the information button height = (EditText) this. findViewById (R. id. editText1); // height input box weight = (EditText) this. findViewById (R. id. editText2); // body weight input box detail = (TextView) this. findViewById (R. id. textView3); // display information // resetInfo of the reset button. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stub // empty weight. setText (""); height. setText (""); detail. setText ("") ;}}); // listen to getInfo. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubIntent intent = new Intent (MainActivity. this, Other. class); // create intent object // Add additional information intent. putExtra ("weight", Double. valueOf (weight. getText (). toString (); intent. putExtra ("height", Double. valueOf (height. getText (). toString (); // you can specify startActivityForResult (intent, 1) ;}}; // process the returned message @ Overrideprotected void onActivityResult (int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubswitch (resultCode) {case RESULT_ OK: // set MainActivity. this. detail. setText (data. getStringExtra ("endMsg"); break; default: break ;}}}
Other. java
Package com. example. inten3; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. widget. button; import android. widget. textView; public class Other extends Activity {private Button returnMsg, resetbut; private TextView weightMsg, heightMsg; private Double weight, height; private String endMsg; @ Overrideprotected void onCreate (Bundle save DInstanceState) {// TODO Auto-generated method stubsuper. onCreate (savedInstanceState); super. setContentView (R. layout. other); returnMsg = (Button) this. findViewById (R. id. otherbutton1); resetbut = (Button) this. findViewById (R. id. otherbutton2); weightMsg = (TextView) this. findViewById (R. id. othertextView1); heightMsg = (TextView) this. findViewById (R. id. othertextView2); // get the Intent object intent Intent = super. getInte Nt (); // obtain the parameter weight = intent. getDoubleExtra ("weight", 0); height = intent. getDoubleExtra ("height", 0); weightMsg. setText ("your weight:" + weight + "KG"); heightMsg. setText ("Your height:" + height + "CM"); returnMsg. setOnClickListener (new View. onClickListener () {// determine and process incoming data input @ Overridepublic void onClick (View v) {// TODO Auto-generated method stubDouble heightNum = height/100; double endNum = weight/(heightNum * HeightNum); if (endNum> = 18 & endNum <= 25) {endMsg = "Your body is normal. Continue to stay close. ";}Else if (endNum <18) {endMsg =" sorry, you cannot lose weight all the time. You are thin now. Be careful ";} else if (endNum> 25 & endNum <= 30) {endMsg = "you are a little fat now, but it doesn't matter. Pay attention to diet ";} else if (endNum> 30 & endNum <= 35) {endMsg = "dear, you are already in obese people. exercise well.";} else if (endNum> 35) {endMsg = "amount, you, you .. How can you do this without losing weight? Be careful not to find the object. ";} // set the parameter to Other on intnet. this. getIntent (). putExtra ("endMsg", endMsg); // sets the return set and specifies the return receiving code Other. this. setResult (RESULT_ OK, Other. this. getIntent (); // end dangqianActivity with Other. this. finish () ;}}); // re-enter the listener resetbut. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stub // sets the return set cancellation status code Other. this. setResult (RESULT_CANCELED, Other. this. getIntent (); // ends the current ActivityOther. this. finish ();}});}}
After learning the basic functions of intent, next we will learn how to call system commands by Intent to complete some other operations.
Next prediction: Intent advanced
I want to help with this book to learn about android and video learning. It is better to give some advice.
I recommend a book, which is very helpful to people who have just started learning. I have learned a lot from myself:
Android SDK development examples
I am a 23-year-old man with no basic knowledge in English, but 26 letters can still be written. How long will it take for me to learn English from scratch?
Perseverance, half a year