Main.xml Layout Code Implementation
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent" > <TextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "@string/hello" /> <!--Create a button event to perform a call - <ButtonAndroid:id= "@+id/mainbtn"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Call" /></LinearLayout>
Second interface layout Second.xml
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent" > <TextViewAndroid:id= "@+id/secondtxt"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Second" /> <!--The second Activity Interface button, the main interface click the corresponding button will jump here to - <ButtonAndroid:id= "@+id/secondbtn"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Second" /></LinearLayout>
Register a second activity interface with Androidmanifest.xml
<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.szy.intent.activity"Android:versioncode= "1"Android:versionname= "1.0"> <USES-SDKandroid:minsdkversion= "8" /> <ApplicationAndroid:icon= "@drawable/icon"Android:label= "@string/app_name"> <!--create a. Mainactivity Activity - <ActivityAndroid:name=". Mainactivity "Android:label= "@string/app_name"> <Intent-filter> <ActionAndroid:name= "Android.intent.action.MAIN" /> <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> </Intent-filter> </Activity> <!--every activity you create needs to be registered here, an activity equivalent to an interface - <ActivityAndroid:name=". Secondactivity "Android:label= "@string/app_name"> </Activity> </Application> <!--indicates the right to call, need mobile phone request to provide - <uses-permissionAndroid:name= "Android.permission.CALL_PHONE"/> <!--permission to send text messages - <uses-permissionAndroid:name= "Android.permission.SEND_SMS"/> </Manifest>
Mainactivity.java Code Implementation
Packagecom.szy.intent.activity;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.Toast; Public classMainactivityextendsactivity{PrivateButton mainbtn=NULL; Private Final Static intRequest_code=1; /**Called when the activity is first created.*/@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.main); MAINBTN=(Button) Findviewbyid (R.ID.MAINBTN); Mainbtn.setonclicklistener (listener); } PrivateOnclicklistener listener=NewOnclicklistener () { Public voidOnClick (View v) {//Intent intent=new Intent (); //intent.setaction (intent.action_sendto);//Intent.setdata (Uri.parse ("smsto:5554"));//Intent.putextra ("Sms_body", "Welcome to ...");//startactivity (intent);Intent intent=NewIntent ();//creates a intent object,Intent.setclass (mainactivity. This, Secondactivity.class);//the object is bound to the secondactivityIntent.putextra ("str", "intent Demo");//passing parameters past, at this time is passing string Internt Demo, and string ID number str//startactivity (intent);Startactivityforresult (Intent, Request_code);//Start passing events, Startactivityforresult used to determine whether to return } }; @Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) { if(Requestcode==request_code)//constants Set when the main interface is sent, non-negative to allow Operation { if(Resultcode==secondactivity.result_code)//a second interface Result_code{Bundle Bundle=data.getextras ();//set up a bundle to receive dataString str=bundle.getstring ("Back");//receive data from the back string ID numberToast.maketext (mainactivity. This, str, toast.length_long). Show ();//displaying data in the main interface } } } }
Secondactivity Code Implementation
Packagecom.szy.intent.activity;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.TextView; Public classSecondactivityextendsactivity{//This result_code is implemented in the main interface Result_code, which means that when the interface returns, it is returned to the interface that result_code the same value as itself. Public Final Static intResult_code=1; PrivateTextView Secondtxt;//Create a TextView to display the data received. PrivateButton secondbtn;//Create a button to trigger an event@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.second); //This is the layout file Second.xml//receive the parameter operation passed by the main interface .Intent intent=getintent (); Bundle Bundle=Intent.getextras (); String Str=bundle.getstring ("str"); //secondtxt Display Contentsecondtxt=(TextView) Findviewbyid (r.id.secondtxt); Secondtxt.settext (str); //Click Secondbtn to trigger the listener eventsecondbtn=(Button) Findviewbyid (R.ID.SECONDBTN); Secondbtn.setonclicklistener (listener); } //trigger content action for listener event PrivateOnclicklistener listener=NewOnclicklistener () { Public voidOnClick (View v) {//Create a intent for clicking on the button, return to the same interface as the Result_code value, pass the parameter to back Data,idIntent intent=NewIntent (); Intent.putextra ("Back" and "Back Data"); Setresult (Result_code, intent); Finish (); } };}
Android--->intent