(1.7)Intent的发送接收Activity返回的结果Intent过滤,接收Android中最重要的特征之一就是可以利用一个带有action的intent使当前app能够跳转到其他app。使用intent来在同一个app的两个activity之间进行切换。通常是定义一个显式(explicit)的intent唤起不同的app来执行某个动作(比如查看地图),则必须使用隐式(implicit)的intent显式(explicit)的intentIntent(ContextpackageContext,Class?cls)隐式(implicit)的intentIntent(Stringaction,Uriuri)常见的ActionACTION_VIEWcontent://contacts/people/1--Displayinformationaboutthepersonwhoseidentifieris1.ACTION_DIALcontent://contacts/people/1--Displaythephonedialerwiththepersonfilledin.ACTION_VIEWtel:123--Displaythephonedialerwiththegivennumberfilledin.NotehowtheVIEWactiondoeswhatwhatisconsideredthemostreasonablethingforaparticularURI.ACTION_DIALtel:123--Displaythephonedialerwiththegivennumberfilledin.ACTION_EDITcontent://contacts/people/1--Editinformationaboutthepersonwhoseidentifieris1.ACTION_VIEWcontent://contacts/people/--Displayalistofpeople,whichtheusercanbrowsethrough.Thisexampleisatypicaltop-levelentryintotheContactsapplication,showingyouthelistofpeople.Selectingaparticularpersontoviewwouldresultinanewintent{ACTION_VIEWcontent://contacts/N}beingusedtostartanactivitytodisplaythatperson.1、用Intent携带数据2、使用startActivityForResult(),而不是startActivity()启动其它Activity3、在onActivityResult()的回调方法里面去接收resultpublicvoidsentMsg(Viewview){//获取输入值EditTexttxtSend=findViewById(R.id.et_send);Stringmsg=txtSend.getText().toString();//准备IntentIntentintent=newIntent(this,LoginActivity.class);intent.putExtra(MSG,msg);//启动ActivitystartActivityForResult(intent,RQ_SEND_MSG);}//定义请求码publicstaticfinalintRQ_SEND_MSG=1;protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){TextViewreceive=findViewById(R.id.txt_receive);if(resultCode!=RESULT_OK){receive.setText(取消操作);return;}switch(requestCode){caseRQ_SEND_MSG:Stringuser=data.getStringExtra(USER);Stringpwd=data.getStringExtra(PASSWORD);receive.setText(登录:+user+/+pwd);break;1、在onCreate方法中接收数据2、使用setResult(RESULT_OK,intent);设置放回码;3、用finish()结束当前方法@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_login);//获取传输的信息Intentintent=getIntent();Stringmsg=intent.getStringExtra(MSG);//显示TextViewtxtMsg=findViewById(R.id.txt_msg);txtMsg.setText(msg);}publicvoidclickOk(Viewview){EditTexttxtUser=findViewById(R.id.et_user);EditTexttxtPwd=findViewById(R.id.et_password);Stringuser=txtUser.getText().toString();Stringpwd=txtPwd.getText().toString();Intentintent=newIntent();intent.putExtra(USER,user);intent.putExtra(PASSWORD,pwd);setResult(RESULT_OK,intent);finish();}publicvoidclickCancel(Viewview){setResult(RESULT_CANCELED);finish();}