14-显示Intent,隐式Intent
来源:互联网 发布:电子相册软件app排行榜 编辑:程序博客网 时间:2024/05/17 02:51
1.显式intent
<pre name="code" class="java"> 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();
<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>的行为测试。
只有当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匹配失败。
- 14-显示Intent,隐式Intent
- 显示Intent,隐式Intent
- 显示Intent,隐式Intent
- 显示Intent,隐式Intent
- Intent—隐式Intent与显示Intent
- 隐式Intent和显示Intent
- Intent(显示使用intent)
- 显示Intent
- Android学习之 Intent显示和Intent隐式
- intent
- Intent
- Intent
- Intent
- intent
- Intent
- intent
- Intent
- Intent
- 使用动软
- Mybatis基础入门(一)——概览
- 关于静态字段方法.const,static的使用
- 创业-互联网
- SGU 261. Discrete Roots (N次剩余)
- 14-显示Intent,隐式Intent
- 关于iCloud的使用
- poj1742
- Hibernate笔记(3)
- ORACLE 分组和时间函数
- 搭建Nginx+Java环境(详细)
- Spring笔记(1)-----Spring资源访问
- 判断单链是否循环,并且找出第一个循环节点
- main函数在c语言中演变