ContentProvider 内容提供者
2015-11-03 20:44
651 查看
内容提供者是android应用程序的主要模块,向应用程序提供内容。通过ContentResolver接口的实现对象来压缩以及提供数据给应用程序。只有在进行应用程序间数据分享时才需要使用内容提供者,否则可以直接使用SQLiteDatabase。ContentProvider是单例模式的,多个程序对其进行操作时,会由同一个ContentProvider进行处理,保证了数据的一致性。
当ContentResolver发出请求时,系统会检查所给的URI的权限并提交给已注册的ContentProvider。ContentProvider总会解析升序部分的URI。而UriMatcher是解析URI的类。
被实现的主要方法有
onCreate() :初始化内容提供者,当其他应用通过ContentResolver第一次访问ContentProvider时,该方法会被回调
query(Uri, String[], String, String[], String) :向调用者返回数据
insert(Uri, ContentValues) :向内容提供者中插入新数据
update(Uri, ContentValues, String, String[]) :更新内容提供者中现有的数据
delete(Uri, String, String[]) 从内容提供者中删除数据
getType(Uri) 返回在内容提供者中的MIME类型数据
操作数据的方法(比如insert和update)可能从任何线程中被调用,其他方法(比如onCreate)只能从应用的主线程中调用。并且需要避免耗时操作。
public ContentProvider()
创建一个ContentProvider实例,必须在清单文件中声明,可以被ContentResolver访问,并且被系统自动创建,因此应用程序一般不直接创建ContentProvider对象。
在创建时,别的对象,大多数方法和变量是没被创建的,子类应该在onCreate()方法中创建自身,而不是通过构造方法。
在应用程序加载完后的主线程中ContentProvider被创建,构造方法不能执行耗时操作,否则应用程序的开启会被延迟。
自定义ContentProvider
1.编写一个ContentProvider的子类
2.实现ContentProvider中的抽象方法。public Bundle call (String method, String arg, Bundle extras)
3.定义ContentProvider的Uri(统一资源标识符)
4.使用UriMatcher对象映射Uri返回代码
5.在AndroidMainfest.xml清单文件中使用标签注册内容提供者。
其他应用程序通过自己的ContentResolver对象调用我们在ContentProvider中重写的方法进行对内容的操作。
UriMatcher的方法
addURI(String authority,String path,int code)//往UriMatcher对象中注册Uri
int match(Uri uri) // 匹配uri的方法。找不到匹配的标识符时,该方法返回-1
当ContentResolver发出请求时,系统会检查所给的URI的权限并提交给已注册的ContentProvider。ContentProvider总会解析升序部分的URI。而UriMatcher是解析URI的类。
被实现的主要方法有
onCreate() :初始化内容提供者,当其他应用通过ContentResolver第一次访问ContentProvider时,该方法会被回调
query(Uri, String[], String, String[], String) :向调用者返回数据
insert(Uri, ContentValues) :向内容提供者中插入新数据
update(Uri, ContentValues, String, String[]) :更新内容提供者中现有的数据
delete(Uri, String, String[]) 从内容提供者中删除数据
getType(Uri) 返回在内容提供者中的MIME类型数据
操作数据的方法(比如insert和update)可能从任何线程中被调用,其他方法(比如onCreate)只能从应用的主线程中调用。并且需要避免耗时操作。
public ContentProvider()
创建一个ContentProvider实例,必须在清单文件中声明,可以被ContentResolver访问,并且被系统自动创建,因此应用程序一般不直接创建ContentProvider对象。
在创建时,别的对象,大多数方法和变量是没被创建的,子类应该在onCreate()方法中创建自身,而不是通过构造方法。
在应用程序加载完后的主线程中ContentProvider被创建,构造方法不能执行耗时操作,否则应用程序的开启会被延迟。
自定义ContentProvider
1.编写一个ContentProvider的子类
2.实现ContentProvider中的抽象方法。public Bundle call (String method, String arg, Bundle extras)
3.定义ContentProvider的Uri(统一资源标识符)
4.使用UriMatcher对象映射Uri返回代码
5.在AndroidMainfest.xml清单文件中使用标签注册内容提供者。
其他应用程序通过自己的ContentResolver对象调用我们在ContentProvider中重写的方法进行对内容的操作。
private static UriMatcher matcher = null; static { // 定义一个Uri匹配器。将UriMatcher.NO_MATCH,即-1作为参数。 matcher = new UriMatcher(UriMatcher.NO_MATCH); // 定义一组匹配规则 // 常量UriMatcher.NO_MATCH表示不匹配任何路径的返回码 matcher.addURI(AUTHORITY, "words", 1); matcher.addURI(AUTHORITY, "newwords", 2); }
UriMatcher的方法
addURI(String authority,String path,int code)//往UriMatcher对象中注册Uri
匹配成功后会返回对应的code值
int match(Uri uri) // 匹配uri的方法。找不到匹配的标识符时,该方法返回-1
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories