学习了ContentProvider,想做个通讯录,结果在Intent和getType(Uri uri)上卡住了,经过查找资料发现了问题的所在。在这里解释下getType(Uri uri)何时被调用,和android.intent.category.DEFAULT 的理解。
我们需要什么时候加android.intent.category.DEFAULT呢?
1、要弄清楚这个问题,首先需要弄明白什么是implicit(隐藏) intent什么是explicit(明确) intent。
Explicit Intent明确的指定了要启动的Acitivity ,比如以下Java代码:
Intent intent= new Intent(this, B.class)
Implicit Intent没有明确的指定要启动哪个Activity ,而是通过设置一些Intent Filter来让系统去筛选合适的Acitivity去启动。
2、intent到底发给哪个activity,需要进行三个匹配,一个是action,一个是category,一个是data。
理论上来说,如果intent不指定category,那么无论intent filter的内容是什么都应该是匹配的。但是,如果是implicit intent,android默认给加上一个CATEGORY_DEFAULT,这样的话如果intent filter中没有android.intent.category.DEFAULT这个category的话,匹配测试就会失败。所以,如果你的 activity支持接收implicit intent的话就一定要在intent filter中加入android.intent.category.DEFAULT。
例外情况是:android.intent.category.MAIN和android.intent.category.LAUNCHER的filter中没有必要加入android.intent.category.DEFAULT,当然加入也没有问题。
我们定义的activity如果接受implicit intent的话,intent filer就一定要加上android.intent.category.DEFAULT这个category。
我的问题就是出现在这里。忘记在intent-filter中加上了<category android:name="android.intent.category.DEFAULT" />了这个。
getType(uri uri)这个方法何时被调用呢?
当我们在程序中需要启动例外的activity时,很多时候我们是使用的隱式調用,即我们不直接指定要跳转的Activity,而是为Intent提供一些相关的参数,让其自动去和AndroidManifest.xml中已有的Activity去匹配,而IntentFilter(Intent 过滤器)在xml中有三個主要的参数:action,categary,data。
我们通过Intent的构造函数或者Intent提供的方法可以指定这个三个参数,如方法有:
intent.setAction(action);
intent.setData(data);
intent.addCategory(category);
等,比如在记事本程序中有:
<intent-filter android:label="@string/resolve_edit">
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.EDIT" />
<action android:name="com.android.notepad.action.EDIT_NOTE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="vnd.android.cursor.item/vnd.google.note" />
</intent-filter>
我们很容易看出action和category是很容易匹配的,而我们传的Uri的数据怎么匹配呢,这是系统会去调用你定义的ContentProvider中的getType,取得相关的返回值來和上面的data串进行匹配,当然getType的返回结果你是需要自己去定义的。
但在程序中你也可以自己知道data的类型,就直接匹配了:intent.setType(type);
分享到:
相关推荐
androdi ContentProvider和Uri详解
<category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <provider android:name=".provider" android:authorities="com.example.tigongzhe.provider" android:multiprocess=...
个人整理的Android Intent的使用介绍 Android 组件之间的信使Intent Android 中的数据存取和ContentProvider
1、Android Room操作SQLite数据 2、ContentProvider使用,使用Room进行数据库操作
ContentProvider远程数据操作(1.0).zip ContentProvider远程数据操作(1.0).zip ContentProvider远程数据操作(1.0).zip ContentProvider远程数据操作(1.0).zip ContentProvider远程数据操作(1.0).zip
android开发contentprovider教学ppt(内部资料).pptx
本篇文章主要介绍了Android学习笔记之ContentProvider和Uri详解,对于学习Android的朋友具有一定的参考价值,有需要可以可以了解一下。
查看源码中的声明: <provider android:name="CallLogProvider" android:authorities="call_log" android:syncable="false" android:multiprocess="false" android:readPermission="android.permission.READ_...
9.6 Implementing a ContentProvider... . 195 9.7 Fast-Forward >>.....196 10 3D Graphics in OpenGL 198 10.1 Understanding 3D Graphics.... 198 10.2 Introducing OpenGL..... 199 10.3 Building an ...
Uri(通用资源标识符 Universal Resource Identifer),代表数据操作的地址,每一个ContentProvider发布数据时都会有唯一的地址。 比如:content://(固定写法)+com.android.contacts(包名,可变)+/contacts(path...
ContentProvider(其它程序共享访问).zip
Android知识点ContentProvider篇
Android中ContentProvider的示例,详细可以参见博客:http://www.cnblogs.com/plokmju/p/android_ContentProvider.html
Android高级编程雪梨作业之自定义ContentProvider 将任务01生词本作业中生成的生词本数据库通过自定义ContentProvider的方式,共享给其他应用。 要求如下: (1) 使用自定义SQLiteOpenHelper来管理数据库; (2) 提交...
android笔记.doc android笔记.pdf 1 系统配置... ..1 1.1 建立SDK开发环境... ...2 1.2 Android源码的编译 ... .6 1.3 编译在G1 上运行的android 2.1(eclair)代码 ... ..9 1.4 编译在G1 上运行的android 2.2(froyo)...
Android学习ContentProvider数据更新与Observer模式一Observer模式意图: 定义对象之间一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被更新。 依赖(Dependents)、发布-...
android 自定义 ContentProvider 以及 ContentResolver
ContentProvider的创建与访问 android
Android ContentProvider简单实现