android Activity隐式跳转

本文介绍了Android中Activity的隐式跳转方法,强调通过在<activity>标签设置<intent-filter>来定义响应的action和category。通过这种方式,可以让其他应用的组件调用并启动指定的Activity。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值