当前位置: 移动技术网 > IT编程>移动开发>Android > 浅谈Android Content Provider的使用

浅谈Android Content Provider的使用

2019年07月24日  | 移动技术网IT编程  | 我要评论

我侬歌,银行活期存款年利率,3dsmax8

content provider:一个组件,必须放在应用的主包或应用的子包之下;

组件的配置需要在清单文件中进行配置;content provider需要在application节点中进行配置;
内容提供者在应用中的作用是对外共享数据(任意类型的数据)使用的,别的程序可以对数据进行crud,如通讯录;
如果采用文件的方式对外共享数据,会因为文件的类型不同而需要使用不同的api访问方式导致访问繁杂,而内容提供者提供了统一的api对数据进行操作;
<provider
  android:name=".personprovider"<!-- 内容提供者类的名称 -->
  android:authorities="cn.wordtech.providers.personprovider"

  android:exported="false" ><!-- 解决 android permission denial error!,在监听内容提供者数据发生变化时需要配置此项 -->
</provider>

另:  
android:authorities:为内容提供者指定一个唯一的标识,这样别的应用才可以唯一获取此provider;

uri 代表了要操作的数据;
uri主要包含两部分的信息:1>>需要操作的contentprovider,2>>对contentprovider中的什么数据进行操作

contentprovider(内容提供者)的scheme已经由android所规定,scheme为:content://
主机名(或authority)用于唯一标识这个contentprovider,外部调用者可以根据此标识来找到它,
路径(path)可以用来表示我们要操作的数据,路径的构建根据业务而定。
ex:
要操作person表中id为10的记录,可以构建这样的路径:/person/10
要操作person表中id为10的记录的name字段,可以构建这样的路径:/person/10/name
要操作person表中的所有记录,可以构建这样的路径:/person
要操作xxx表中的记录,可以构建这样的路径:/xxx
要操作的数据不一定是数据库中的文件,也可以是文件,xml或网络等其它方式
ex:
要操作xml文件中person节点下的name节点,可以构建这样的路径:/person/name

复制代码 代码如下:

public class personprovider extends contentprovider {// content provider需要继承自contentprovider类
 // 删改查中,都有两种情况:
 // person 对整个表进行操作
 // person/id 对表中的与id对应记录进行操作
 private dbopenhelper dbopenhelper;
 private static final urimatcher matcher = new urimatcher(urimatcher.no_match);// new urimatcher(code);code即为匹配不成功时返回的值;
 private static final int persons = 1;
 private static final int person = 2;
 // 设置匹配项
 static {
  matcher.adduri("cn.wordtech.providers.personprovider", "person",persons);
  matcher.adduri("cn.wordtech.providers.personprovider", "person/#",person);// #号表示数字
 }
 // content://cn.wordtech.providers.personprovider/person
 @override
 public boolean oncreate() {
  // 由系统调用,当contentprovider的实例被创建出来的时候被调用,android开机后,当第一次有应用访问contentprovider时才创建contentprovider;
  dbopenhelper = new dbopenhelper(getcontext(), 1);
  return false;
 }

 // 可以供外部的应用查询数据,返回查询得到的游标对象
 @override
 public cursor query(uri uri, string[] projection, string selection,
   string[] selectionargs, string sortorder) {
  sqlitedatabase db = dbopenhelper.getwritabledatabase();
  switch (matcher.match(uri)) {
  case 1:
   return db.query("person", projection, selection, selectionargs,
     null, null, sortorder);
  case 2:
   long rowid = contenturis.parseid(uri);// 返回要操作的id
   string where = "personid=" + rowid;
   if (selection != null && !"".equals(selection.trim())) {
    where += "and" + selection;
   }
   return db.query("person", projection, where, selectionargs, null,
     null, sortorder);

  default:
   throw new illegalargumentexception("");
  }
 }

 // 此方法用于返回目前uri所代表的数据的mime类型,
 // 如果操作的数据属于集合类型,则mime字符串就以"vnd.android.cursor.dir"开头
 // 如果操作的数据属于非集合类型,则mime字符串就以"vnd.android.cursor.item"开头
 @override
 public string gettype(uri uri) {
  switch (matcher.match(uri)) {
  case 1:
   return "vnd.android.cursor.dir/person";
  case 2:
   return "vnd.android.cursor.item/person";
  default:
   throw new illegalargumentexception("");
  }
 }

 // 此方法需要返回操作记录对应的uri
 @override
 public uri insert(uri uri, contentvalues values) {
  sqlitedatabase db = dbopenhelper.getwritabledatabase();
  switch (matcher.match(uri)) {
  case 1:
   long rowid = db.insert("person", "", values);// 返回行号?主键值
   // uri inserturi = uri
   // .parse("content://com.sqlite.personprovider/person/"
   // + rowid);
   uri inserturi = contenturis.withappendedid(uri, rowid);
   return inserturi;
  default:
   throw new illegalargumentexception("this is unknow uri:" + uri);
  }

 }

 // 返回受影响的行数
 @override
 public int delete(uri uri, string selection, string[] selectionargs) {
  sqlitedatabase db = dbopenhelper.getwritabledatabase();
  int num = 0;
  switch (matcher.match(uri)) {
  case 1:
   num = db.delete("person", selection, selectionargs);// 清空整个表
   break;
  case 2:
   long rowid = contenturis.parseid(uri);// 返回要操作的id
   string where = "personid=" + rowid;
   if (selection != null && !"".equals(selection.trim())) {
    where += "and" + selection;
   }
   num = db.delete("person", where, selectionargs);
   break;
  default:
   throw new illegalargumentexception("");
  }
  return num;
 }

 @override // 返回受影响的行数
 public int update(uri uri, contentvalues values, string selection,string[] selectionargs) {
  sqlitedatabase db = dbopenhelper.getwritabledatabase();
  int num = 0;
  switch (matcher.match(uri)) {
  case 1:
   num = db.update("person", values, selection, selectionargs);
   break;
  case 2:
   long rowid = contenturis.parseid(uri);// 返回要操作的id
   string where = "personid=" + rowid;
   if (selection != null && !"".equals(selection.trim())) {
    where += "and" + selection;
   }
   num = db.update("person", values, where, selectionargs);
   break;
  default:
   throw new illegalargumentexception("");
  }
  return num;
 }


下面是对前一个类进行测试
复制代码 代码如下:

public class accesscontentprovidertest extends androidtestcase {
 public void testinsert() {
  uri uri = uri.parse("content://cn.wordtech.providers.personprovider/person");// 根据标识名得到内容提供者
  contentresolver cr = this.getcontext().getcontentresolver(); // this class provides applications access to the content model
  contentvalues values = new contentvalues();
  values.put("name", "livingstone");
  values.put("phone", "110");
  values.put("amount", "1111111111");
  cr.insert(uri, values);// 在cr的内部会调用内容提供者的值;
 }

 public void testdelete() {
  uri uri = uri.parse("content://cn.wordtech.providers.personprovider/person/1");// 根据标识名得到内容提供者
  contentresolver cr = this.getcontext().getcontentresolver();
  cr.delete(uri, null, null);
 }

 public void testupdate() {
  uri uri = uri.parse("content://cn.wordtech.providers.personprovider/person/2");// 根据标识名得到内容提供者
  contentresolver cr = this.getcontext().getcontentresolver();
  contentvalues values = new contentvalues();
  values.put("name", "livingstone11");
  cr.update(uri, values, null, null);
 }

 public void testquery() {
  uri uri = uri.parse("content://cn.wordtech.providers.personprovider/person");// 根据标识名得到内容提供者
  contentresolver cr = this.getcontext().getcontentresolver();
  cursor cursor = cr.query(uri, null, null, null, "personid asc");
  while (cursor.movetonext()) {
   string name = cursor.getstring(cursor.getcolumnindex("name"));
   log.i("name", name);
  }
 }
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网