Activity跳转有2种方式:
1.显示
略
2.隐式
通过在<activity>标签下配置<intent-filter>的内容,可以指定当前活动能够响应的action
和category,打开AndroidManifest.xml,添加如下代码:
<activity android:name=".SecondActivity" >
<intent-filter>
<action android:name="com.example.activitytest.ACTION_START" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
在<action> 标签中我们指明了当前活动可以响应com.example.activitytest.ACTION_
START 这个action,而<category>标签则包含了一些附加信息,更精确地指明了当前的活动
能够响应的Intent 中还可能带有的category。只有<action>和<category>中的内容同时能够匹
配上Intent 中指定的action 和category 时,这个活动才能响应该Intent。
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("com.example.activitytest.ACTION_START");
startActivity(intent);
}
});
每个Intent 中只能指定一个action,但却能指定多个category。
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("com.example.activitytest.ACTION_START");
intent.addCategory("com.example.activitytest.MY_CATEGORY");
startActivity(intent);
}
});
在<intent-filter>中再添加一个category 的声明
<activity android:name=".SecondActivity" >
<intent-filter>
<action android:name="com.example.activitytest.ACTION_START" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.example.activitytest.MY_CATEGORY"/>
</intent-filter>
</activity>
更多隐式Intent 的用法
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.baidu.com"));
startActivity(intent);
}
});
Intent 的action 是Intent.ACTION_VIEW,这是一个Android 系统内
置的动作,其常量值为android.intent.action.VIEW。然后通过Uri.parse()方法,将一个网址字
符串解析成一个Uri 对象,再调用Intent 的setData()方法将这个Uri 对象传递进去。
我们还可以在<intent-filter>标签中再配置一个<data>标签,用于更精确地指
定当前活动能够响应什么类型的数据。<data>标签中主要可以配置以下内容。
1. android:scheme
用于指定数据的协议部分,如上例中的http 部分。
2. android:host
用于指定数据的主机名部分,如上例中的www.baidu.com 部分。
3. android:port
用于指定数据的端口部分,一般紧随在主机名之后。
4. android:path
用于指定主机名和端口之后的部分,如一段网址中跟在域名之后的内容。
5. android:mimeType