显示Intent,隐式Intent

Intent 分为两种显示Intent,隐式Intent。


1.显式intent

        Intent intent = new Intent(context, NavigationActivity.class);
        intent.putExtra(Constants.INVALID_USER, true);
        context.startActivity(intent);

需要明确指定需要激活的Activity.


2.隐式Intent

在使用Intent进行跳转时,没有明确指定跳转的Activity或者Service.通过Intent-Filter(Intent过滤器)进行匹配过滤,会跳转到符合匹配条件的Activity或者Service。如果有多个同时匹配,会弹出对话框,供用户来选择激活哪个组件,如调用手机浏览器。


隐式Intentd的使用如下:


首先在清单文件AndroidManifest.xml的Activity声明中添加Intent-filter

<activity>
	<intent-filter>
		<action android:name="...."/>
		<category android:name="...."/>
		<category android:name="android.intent.category.DEFAULT"/>	
		<data android:scheme="..." android:host="..." android:path="/..." android:type="..."/>
	</intent-filter>
</activity>


然后在需要跳转的地方添加

Intent intent = new Intent();
intent.setAction("....");
intent.addCategory("....");
intent.setData(Uri.parse("...."));
//设置data的scheme、host、path条件
intent.setDataAndType(Uri.parse(""),String type);
//同时设置data的scheme、host、path、type条件
startActivity(intent);


注意:如果在Intent-filter中的data中多了一个android:mimeType="text/*",此时在跳转的地方不能使用intent.setData,而要使用intent.setDataAndType();


详细说下intent-filter

 <activity
            android:name="com.tencent.tauth.AuthActivity"
            android:noHistory="true"
            android:launchMode="singleTask">
            <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:scheme="tencent100559646" />
            </intent-filter>
        </activity>

action 动作

  一条<intent-filter>元素至少应该包含一个<action>,否则任何Intent请求都不能和该<intent-filter>匹配。

  如果Intent请求的Action和<intent-filter>中个某一条<action>匹配,那么该Intent就通过了这条<intent-filter>的动作测试。

  如果Intent请求或<intent-filter>中没有说明具体的Action类型,那么会出现下面两种情况。

  (1) 如果<intent-filter>中没有包含任何Action类型,那么无论什么Intent请求都无法和这条<intent-filter>匹配;

  (2) 反之,如果Intent请求中没有设定Action类型,那么只要<intent-filter>中包含有Action类型,这个Intent请求就将顺利地通过<intent-filter>的行为测试。


category 类别

只有当Intent请求中所有的Category与组件中某一个Intent-filter的<category>完全匹配时,才会让该Intent请求通过测试,Intent-filter中多余的<category>声明并不会导致匹配失败。即只要intent-filter中德category包含intent请求时设置的category即可。

android.intent.category.DEFAULT的作用

每一个通过 startActivity()方法发出的隐式 Intent 都至少有一个 category,就是 "android.intent.category.DEFAULT",所以只要是想接收一个隐式Intent 的 Activity 都应该包括 "android.intent.category.DEFAULT" category,不然将导致 Intent匹配失败。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值