Android开发(实验四)实验题目:Activity生命周期验证试验指导老师:班级:计算机科学与技术系班姓名:(一、实验目的1、深入理解Activity运行机制和原理。2、掌握Activity的声明周期各个阶段工作流程。3、掌握多个Activity之间生命周期的转换过程。4、通络Intent实现多页面之间数据的传递(辅助完成)。二、实验内容1、搭建任意组件,设计多个界面,运行程序观察其生命周期的情况。2、使用Intent实现页面之间数据的传递。三、实验代码AlifecyfleActivity.javapackagecom.wr;importandroid.app.Activity;importandroid.content.Intent;importandroid.os.Bundle;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.widget.Button;publicclassAlifecycleActivityextendsActivity{/**Calledwhentheactivityisfirstcreated.*/@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);ButtonBtnStart=(Button)findViewById(R.id.BtnStart);BtnStart.setOnClickListener(newMyBtnClickListener());}@OverrideprotectedvoidonDestroy(){//TODOAuto-generatedmethodstubsuper.onDestroy();System.out.println(AlifecycleActivity:onDestroy());}@OverrideprotectedvoidonPause(){//TODOAuto-generatedmethodstubsuper.onPause();System.out.println(AlifecycleActivity:onPause());}@OverrideprotectedvoidonRestart(){//TODOAuto-generatedmethodstubsuper.onRestart();System.out.println(AlifecycleActivity:onRestart());}@OverrideprotectedvoidonResume(){//TODOAuto-generatedmethodstubsuper.onResume();System.out.println(AlifecycleActivity:onResume());}@OverrideprotectedvoidonStart(){//TODOAuto-generatedmethodstubsuper.onStart();System.out.println(AlifecycleActivity:onStart());}@OverrideprotectedvoidonStop(){//TODOAuto-generatedmethodstubsuper.onStop();System.out.println(AlifecycleActivity:onStop());}publicclassMyBtnClickListenerimplementsOnClickListener{//重写点击事件处理方法publicvoidonClick(Viewarg0){//TODOAuto-generatedmethodstubIntentintent=newIntent(AlifecycleActivity.this,AlcActivity2.class);//创建意图对象//使用意图对象打开一个新的ActivitystartActivity(intent);}}}AlcActivity.javapackagecom.wr;importandroid.app.Activity;importandroid.os.Bundle;publicclassAlcActivity2extendsActivity{/**Calledwhentheactivityisfirstcreated.*/@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.alcactivity2_main);}@OverrideprotectedvoidonDestroy(){//TODOAuto-generatedmethodstubsuper.onDestroy();System.out.println(AlcActivity2:onDestroy());}@OverrideprotectedvoidonPause(){//TODOAuto-generatedmethodstubsuper.onPause();System.out.println(AlcActivity2:onPause());}@OverrideprotectedvoidonRestart(){//TODOAuto-generatedmethodstubsuper.onRestart();System.out.println(AlcActivity2:onRestart());}@OverrideprotectedvoidonResume(){//TODOAuto-generatedmethodstubsuper.onResume();System.out.println(AlcActivity2:onResume());}@OverrideprotectedvoidonStart(){//TODOAuto-generatedmethodstubsuper.onStart();System.out.println(AlcActivity2:onStart());}@OverrideprotectedvoidonStop(){//TODOAuto-generatedmethodstubsuper.onStop();System.out.println(AlcActivity2:onStop());}}IntentActivity.javapackagecom.gml;importandroid.app.Activity;importandroid.content.Intent;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;importandroid.widget.TextView;publicclassIntentActivityextendsActivity{privateTextViewtxtShow;privateButtonbtnPage2;privateStringname;privateintage;/**Calledwhentheactivityisfirstcreated.*/@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);txtShow=(TextView)findViewById(R.id.txtShow);btnPage2=(Button)findViewById(R.id.btnPage2);btnPage2.setOnClickListener(myListener);name=Mary;age=27;Strings=姓名:+name+\n\r+年龄:+age;txtShow.setText(s);}@Override//接收被调程序传回的数据,必须重载onActiveResult()方法。protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){//TODOAuto-generatedmethodstubsuper.onActivityResult(requestCode,resultCode,data);if(requestCode==ACTIVITY_EDIT){if(resultCode==RESULT_OK){Bundlebundle=data.getExtras();age=bundle.getInt(AGE);Strings=姓名:+name+\n\r+年龄:+age;txtShow.setText(s);}if(resultCode==RESULT_CANCELED);}}privatestaticfinalintACTIVITY_EDIT=1;privateButton.OnClickListenermyListener=newButton.OnClickListener(){publicvoidonClick(Viewv){Intentintent=newIntent();//使用setClass()确定Intent的发件人和收件者,IntentActivity传递给Second.intent.setClass(IntentActivity.this,OneActivity.class);//使用Bundle对象的put()方法打包数据,并利用putExtras()来进行传递Bundlebundle=newBundle();bundle.putString(NAME,name);bundle.putInt(AGE,age);intent.putExtras(bundle);//用startActivityForResult()方法取代startAcctivity()方法将包里寄出,并等待另一方的回音。startActivityForResult(intent,ACTIVITY_EDIT);}};}OneActivity.javapackagecom.gml;importandroid.app.Activity;importandroid.content.Intent;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;importandroid.widget.EditText;importandroid.widget.TextView;publicclassOneActivityextendsActivity{EditTextedtAge;intage;publicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.page1);TextViewtxtName=(TextView)findViewById(R.id.txtName);edtAge=(EditText)findViewById(R.id.edtAge);ButtonbtnSure=(Button)findViewById(R.id.btnSure);ButtonbtnCancel=(Button)findViewById(R.id.btnCancel);btnS