Android|Intent组件使用
之前学习 ListView,RecyclerView 时己经多次使用了 Intent,当一个 Activity 需要启动另一个 Activity 时, 程序并没有直接告诉系统要启动哪个 Activity,而是通过 Intent 来表达自己的意图:需要启动哪个 Activity。
Intent不仅是封装Android应用程序需要启动某个组件的“意图”,还是应用程序组件之间通信的重要媒介。
Intent 介绍
Intent 的作用
- 启动 Activity
- Activity 表示应用中的一个屏幕。通过将 Intent 传递给 startActivity(),可以启动新的 Activity 实例。Intent 描述了要启动的 Activity,并携带了任何必要的数据。
- 如果希望在 Activity 完成后收到结果,则可以调用 startActivityForResult()。在 Activity 的 onActivityResult() 回调中,Activity 将结果作为单独的 Intent 对象接收。
- 启动服务
- Service 是一个不使用用户界面而在后台执行操作的组件。通过将 Intent 传递给 startService(),可以启动服务执行一次性操作(例如,下载文件)。Intent 描述了要启动的服务,并携带了任何必要的数据。
- 如果服务旨在使用客户端-服务器接口,则通过将 Intent 传递给 bindService(),可以从其他组件绑定到此服务。
- 发送广播
- 广播是任何应用均可接收的消息。系统将针对系统事件(例如:系统启动或设备开始充电时)传递各种广播。通过将 Intent 传递给 sendBroadcast()、sendOrderedBroadcast() 或 sendStickyBroadcast(),可以将广播传递给其他应用。
Intent 类型
显式 Intent
显式 Intent,即在通过 Intent 启动 Activity 时,需要明确指定激活组件的名称。在程序中,如果需要在本应用中启动其他的 Activity 时,可以使用显式意图来启动 Activity,其本例代码具体如下:
Intent intent=new Intent(MainActivity.this,activity_third.class);
startActivity(intent);
通过 Intent 的构造方法来创建 Intent 对象。
构造方法接收两个参数:
- 第一个参数 Context 要求提供一个启动 Activity 的上下文
- 第二个参数 Class 则是指定要启动的目标 Activity
除了通过指定类名开启组件外,显式 Intent 还可以根据目标组件的包名、全路径名来指定开启组件,
Intentintent=new Intent();
intent.setClassName("top.zzgpro.calculate.MainActivity", "top.zzgpro.calculate.SecondActivity");
startActivity(intent);
startActivity()
Activity 类中提供了一个 startActivity ( Intent intent )方法,该方法专门用于开启 Activity,它接收一个 Intent 参数,这里将构建好的 Intent 传入该方法即可启动目标 Activity。
隐式 Intent
没有明确指定组件名的 Intent 称为隐式 Intent,又叫隐式意图。Android 系统会根据隐式 Intent 中设置的动作(action )、类别(category )、数据(Uri 和数据类型)找到最合适的组件。
<activity
android:name=".three"
android:exported="false"
>
<intent-filter>
<action android:name="top.zzgpro.top.JumpThree"></action>
<category android:name="android.intent.category.DEFAULT"></category>
</intent-filter>
</activity>
Intent intent=new Intent();
intent.setAction("top.zzgpro.top.JumpThree");
startActivity(intent);
隐式 Intent 的工作原理
在上图中,Activity A 创建包含操作描述的 Intent,并将其传递给 startActivity()。 Android 系统搜索所有应用中与 Intent 匹配的 Intent 过滤器。 找到匹配项之后,该系统通过调用匹配 Activity(Activity B)的 onCreate() 方法并将其传递给 Intent,以此启动匹配 Activity。
在上述两种Intent中,显式Intent开启组件时必须要指定组件的名称,一般只在本应用程序切换组件时使用。而隐式Intent的功能要比显示Intent更加强大,不仅可以开启本应用的组件,还可以开启其他应用的组件,例如打开系统自带的照相机、浏览器等。
Intent 在 Activity 间传递数据
Intent 可以用来开启 Activity,同样它也可以用来在 Activity 之间传递数据。Intent 提供了多个重载的方法来携带额外的数据,如下所示。
putExtra(String name, xxx value):向 Intent 中按 key-value 对的形式存入数据。
getXxxExtra(String name):从 Intent 中按 key 取出指定类型的数据。
putExtras(Bundle data):向 Intent 中放入需要携带的数据包。
Bundle getExtras():取出 Intent 中所携带的数据包。
使用 Intent 传递数据只需调用 putExtra()方法将想要存储的数据存在 Intent 中即可。当启动了另一个 Activity 后,再把这些数据从 Intent 中取出即可。其核心示例代码如下:
//往其他Activity传数据
Bundle bundle=new Bundle();
bundle.putString("username",Account);
bundle.putString("password",Password);
Intent intent=new Intent(Register2.this,Login.class);
intent.putExtra("bundle",bundle);
startActivity(intent);
//接收数据,在Oncreate方法中
Intent intent=getIntent();
Bundle bundle=intent.getExtras();
String stuName=bundle.getString("name");
int stuAge=bundle.getString("age");
Activity 数据回传
Activity 还提供了一个 startActivityForResult(Intent intent, int requestCode) 方法来启动其他 Activity。
该方法用于启动指定 Activity,而且期望获取指定 Activity 返回的结果。
为了获取被启动的 Activity 所返回的结果,需要从以下三方面着手。
使用 startActivityForResult(Intent intent, int requestCode) 方法启动指定 Activity。
当前 Activity 需要重写 onActivityResult(int requestCode,int resultCode,Intent intent), 当被启动的 Activity 返回结果时,该方法将会被触发,其中 requestCode 代表请求码, 而 resultCode 代表 Activity 返回的结果码,这个结果码也是由开发者根据业务自行设定的。
被启动的 Activity 需要调用 setResult()方法设置处理结果。
关于启动 Activity 并回传数据的核心代码如下所示:
Toast.makeText(Register2.this,"register finished",Toast.LENGTH_SHORT).show();
Bundle bundle=new Bundle();
bundle.putString("username",Account);
bundle.putString("password",Password);
Intent intent=new Intent(Register2.this,Login.class);
intent.putExtra("bundle",bundle);
setResult(1,intent);
Register2.this.finish();
以上代码设置 Activity 要带回的数据,并利用 Intent 带回数据
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(1==resultCode){
Bundle bundle= data.getBundleExtra("bundle");
String name=bundle.getString("username");
String password=bundle.getString("password");
Log.i("RegisterCallBack",name+" "+password);
EditText usernameInput=(EditText) findViewById(R.id.username);
EditText passwordInput=(EditText) findViewById(R.id.password);
usernameInput.setText(name);
passwordInput.setText(password);
}
}
第一个 Activity 重写 OnActivityResult 方法,自定义当第二个 Activity 返回时的处理逻辑
hexo test