Learn android from scratch (Intent preliminary. 40 .)

Source: Internet
Author: User

Learn android from scratch (Intent preliminary. 40 .)

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

     
      
  
 

Jump interface Xml

     
  
 


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.


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PGJyPgo8L3A + CjxwPjxwcmUgY2xhc3M9 "brush: java;">

Other. xml

 
     
      
  
 



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 ();}});}}





System commands to complete some other operations


Next prediction: Intent advanced

Related Article

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.