一.活动的跳转:Intent
1.显式Intent
右键点击com.example.activitytest,创建一个新的Empty Activity(new/Activity/Empty Activity)。
在Second_layout.xml里面修改布局为LinearLayout,添加button控件,命名为Button 2,并修改部分代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 2"
/>
</LinearLayout>
回到FirstActivity.java,修改点击事件,使用Intent构造函数中的Intent(Context packageContext,Class<?>cls),所使用的构造函数需要传入两个参数:
- 第一个参数Context提供一个启动活动的上下文
- 第二个参数Class用于指定想要启动的目标活动
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(FirstActivity.this,SecondAcitivity.class);
startActivity(intent);
}
});
现在就能实现由FirstActivity到SecondActivity页面的跳转。
显式Intent意图明确指定了要激活的组件是哪个组件,一般是在应用程序内部使用。效率高,耦合度高。
2.隐式Intent
1 )了解隐式Intent
在AndroidManifest.xml中SecondActivity的< intent-filter >标签内添加代码(修改注册信息):
<action android:name="com.example.activitytest.ACTION_START" />
<category android:name="android.intent.category.DEFAULT" />
- < action >标签指明当前活动可响应的action(活动)
- < category >标签则用来更加精确指明可响应的Intent中可能带有的category(类别)
- 只有< action >和< category >同时匹配才能响应
修改FirstActivity.java中点击事件的代码:
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("com.example.activitytest.ACTION_START");
startActivity(intent);
}
});
#每个Intent只能指定一个action,但是可以指定多个category
#每新增加一个category,都需要在< intent-filter >标签内添加相应的声明
2 )隐式Intent的更多用法
隐式Intent使得多个应用程序之间的功能共享成为可能(比如转发功能的实现)
使用Intent打开网页。修改FirstActivity中点击事件的代码:
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://cn.bing.com"));
startActivity(intent);
}
});
此处指定了Intent的action是Intent.ACTION_VIEW
(一个Android内置常量),通过Uri.parse()
方法将网址解析成Uri对象,然后调用Intent的setData()
方法这个Uri对象传出去(打开网页)
#< data >标签用于更加精确的指定当前活动能够响应什么类型的数据
属性 | 作用 |
---|---|
android:scheme | 用于指定数据的协议部分,如http://cn.bing.com中的http部分。 |
android:host | 用于指定数据的主机名部分,如http://cn.bing.com中的www.baidu.com部分。 |
android:port | 用于指定数据的端口部分,一般紧随在主机名之后,如http://www.rowyer:8080/mypath中的8080部分。 |
android:path | 用于指定主机名和端口之后的部分,如一段网址中跟在域名之后的内容,如http://www.rowyer:8080/mypath中的/mypath部分。 |
android:mimeType | 用于指定可以处理的数据类型,允许使用通配符的方式进行指定。 |
在点击BUTTON 1
之后会直接跳转到必应搜索页面。
#直观感受action+category+data的三重指定
新建ThirdActivity,修改third_layout.xml中的代码为:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 3"
/>
</LinearLayout>
在AndroidManifest.xml中修改ThirdActivity的注册信息:
<activity android:name=".ThirdActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="http"/>
</intent-filter>
</activity>
在ThirdActivity的< intent-filter >中的配置:
- 活动能响应的action:
intent.action.VIEW
- 活动的category指定
默认的category值
- < data >标签指定数据的协议必须的http协议
这样ThirdActivity就能像浏览器一样响应一个打开网页的Intent,如图:
点击浏览器打开必应搜索页面,点击ActivityTest打开ThirdActivity。
Intent还能在程序中调用系统拨号界面,地理位置显示等等,只是指定Intent的action不同,data指定的协议不同。
隐式Intent的效率较低,耦合度低
小结:显式Intent和隐式Intent实现活动跳转。显式Intent直观明了,直接指向跳转的界面;隐式Intent可以通过多重指定来给出符合要求的多个界面以供选择,还可以启动其他程序的活动。