Intent intent is a very important part of Android, and he has a wide range of applications in Activity,service.
1 |
public void StartActivity (Intent Intent) |
Ordinary |
Initiates an activity and transmits data via intent |
2 |
public void Startactivityforresult (Intent Intent, int requestcode) |
Ordinary |
Start and receive another activity program callback data, when Requestcode greater than 0 can trigger Onactivityresult () |
3 |
Public Intent getintent () |
Ordinary |
Returns the intent that initiates the current Activity program |
4 |
protected void Onactivityresult (int requestcode, int resultcode, Intent data) |
Ordinary |
Override this method to process the callback operation when it is necessary to receive the intent callback data |
5 |
public void Finish () |
Ordinary |
Calling this method returns the previous activity program and automatically calls the Onactivityresult () method |
6 |
Public final Cursor managedquery (Uri Uri, string[] projection, string selection, string[] Selectionargs, String sortOrder ) |
Ordinary |
Processing the returned cursor result set |
The next step is to use examples to explain
The simplest of 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/activity_horizontal_ Margin "android:paddingright=" @dimen/activity_horizontal_margin "android:paddingtop=" @dimen/activity_vertical_ Margin "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" android:layout_marginbottom= "141DP" android:text= "Jump"/></relativelayou T>
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/activity_ Horizontal_margin " android:paddingright=" @dimen/activity_horizontal_margin " android:paddingtop=" @dimen /activity_vertical_margin " tools:context=". Mainactivity "> <textview android:layout_width=" wrap_content " android:layout_height=" Wrap_ Content " android:text=" I am the second screen interface "/></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 Intent Object StartActivity (intent);// Open IntentMainActivity.this.finish ();//Make Current Activity end});}}
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, take a look at the intent that can pass the 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:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". Mainactivity "> <button android:id=" @+id/button1 "android:layout_width=" Wrap_content "Androi d: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_centerhorizo Ntal= "true" android:ems= "ten" 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/activity_horizontal_ Margin "android:paddingright=" @dimen/activity_horizontal_margin "android:paddingtop=" @dimen/activity_vertical_ Margin "tools:context=". Mainactivity "> <button android:id=" @+id/button1 "android:layout_width=" Wrap_content "Androi d: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_centerhorizo Ntal= "true" android:ems= "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:o rientation= "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);//Set Intent Intent.putextra ("info", Info.gettext (). ToString ());//For the purpose of adding additional information startactivity (intent);//Start Intent MainActivity.this.finish ();//Terminate 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 Intent Object Button.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method stubstring Info=intent.getstringextra ("info");//Gets the data passed Text.settext ( info);//Display Data}});}}
Passing and returning data using intent
A program for adult height and weight testing with intent
BMI body Mass index = weight (kg) in addition to height (m) squared kg/m2 normal weight: BMI = 18-25 Overweight : BMI = 25-30 mild obesity: Body mass index > 30 Obesity: BMI > 35 Severe obesity:
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 Information button getinfo= (button) This.findviewbyid (r.id.button2);// Get Info button height= (EditText) This.findviewbyid (R.ID.EDITTEXT1);//Height input box weight= (EditText) This.findviewbyid ( R.ID.EDITTEXT2)///Weight input box detail= (TextView) This.findviewbyid (R.ID.TEXTVIEW3);//information display//reset button monitoring Resetinfo.setonclicklistener (New View.onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated Method stub//set them all to null WEight.settext (""); Height.settext ("");d Etail.settext ("");}); /Get Info button for Listener getinfo.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-g Enerated 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 ()));//set start and pass request code Startactivityforresult (intent, 1);}); }//processing returned information @overrideprotected void Onactivityresult (int requestcode, int resultcode, Intent data) {//TODO Auto-generat Ed method Stubswitch (ResultCode) {case result_ok://set information 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 savedinstancestate) {//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);// Gets the Intent object Intent Intent = super.getintent ();//Get 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 () {///for incoming data input judgment and processing @overridepublic void OnClick (View v) {//TODO Auto-generated method Stubdouble Heightnum = height/100;double Endnum = weight/(Heightnum * heightnum); if (Endnum > = && Endnum <=) {endmsg = "your body is normal. Oh, keep your kiss." ";} else if (Endnum <) {endmsg = "Pro Ah, can't always lose weight ah, you are now thin oh, beware of the Body";} else if (Endnum > <= && endnum) {ENDMS g = "You are a little fat now, but it doesn't matter, pay attention to Diet Oh";} else if (Endnum > && endnum <=) {endmsg = "Pro, you have entered the obese crowd oh, pay attention to exercise oh";} else if (Endnum > +) {endmsg = "Well, you, you." How can you do this, not to lose weight, be careful to find the object Oh ";} Set the parameters to Intnet on Other.this.getIntent (). PutExtra ("Endmsg", endmsg);//sets the return set and sets the return acceptance code Other.this.setResult (RESULT_OK , Other.this.getIntent ());//dangqianactivity End off Other.this.finish ();}); /re-enter Listener Resetbut.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {//TODO Auto-generated method stub//Set returns the collection cancellation status code Other.this.setResult (result_canceled, Other.this.getIntent ());// End current ActivityOther.this.finish ();}});}}
Learn the basic use of intent, next we will learn intent call system instructions to complete some other operations
Forecast for next section: Intent Advanced
Start from scratch to learn android<intent preliminary. 40 .>