Android中一个Activity多个intent-filter的调用方法

本文介绍了在Android开发中如何使用<intent-filter>来定义Activity的调用方式。通过示例展示了如何设置action、category及data属性,并解释了如何通过Intent启动相应的Activity。

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

在Android中,Activity允许有很多种调用方式,其中一个方法是使用 <intent-filter>。

比如:

<intent-filter>
	<action android:name="android.intent.action.VIEW" />
	<category android:name="android.intent.category.DEFAULT" />
	<category android:name="android.intent.category.BROWSABLE" />
	<data
		android:host="www.google.com"
		android:path="m/products/scan"
		android:scheme="http" />
</intent-filter>


一开始我以为,在浏览器中键入“http://www.google.com/m/products/scan”就可以调用了的,结果发现浏览器只是正常打开它 T.T

后来发现,它的调用仍然需要使用intent

Uri uri = Uri.parse("http://www.google.com/m/products/scan");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);

我是在一个Button的onClick()方法里写的这些代码,这样按这个按钮,就会调用所有符合要求的含有对应<intent-filter>的Activity,在我的手机里,有Chrome Beta、浏览器、快拍二维码、条码扫描器,以及我刚刚创建那个应用的Activity。


嗯嗯,记录一下,以后会经常把日常遇到的问题记下来,方便遇到同样问题的开发者们一起学习!

by HADB of HAOest

2012-6-18 23:13 于宿舍

我的原文地址:http://blog.haoest.com/techpost/several-intent-filters-in-one-activity.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值