google设计内容提供者的目的:
为外部应用程序提供了统一的访问数据库的方式。
编写一个内容提供者的步骤:
1、在工程中添加一个内容提供者的类,继承了ContentProvider类型,实现了增删改查的方法;
2、在清单文件中配置一个provider节点,指定authorities(相当于网站的域名,用来唯一标示一个内容提供者)、exported为true(从4.1开始,exported没有指定为true,表示其他应用程序不能访问这个内容提供者);
关键代码:
//mUriMatcher uri匹配,NO_MATCH 匹配uri时如果没有匹配成功返回NO_MATCH
private static UriMatcher mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
//mUriMatcher的岗前培训 content://com.itheima.accountdb.AccountContentProvider/insert
static{
//添加可以匹配到的uri
//authority 内容提供者的主机名
//path 访问的路径
//code 匹配,当匹配成功时会返回这个匹配码
mUriMatcher.addURI("com.itheima.accountdb.AccountContentProvider", "insert", INSERT);
mUriMatcher.addURI("com.itheima.accountdb.AccountContentProvider", "delete", DELETE);
mUriMatcher.addURI("com.itheima.accountdb.AccountContentProvider", "query", QUERY);
mUriMatcher.addURI("com.itheima.accountdb.AccountContentProvider", "update", UPDATE);
}
配置文件:
android:name="com.itheima.accountdb.AccountContentProvider"
android:authorities="com.itheima.accountdb.AccountContentProvider"
android:exported="true" >
内容提供者工作的原理:内容提供者解析器是通过uri匹配要访问的内容提供者和访问路径的;