当前位置: 移动技术网 > IT编程>移动开发>Android > Android编程使用内容提供者方式(ContentProvider)进行存储的方法

Android编程使用内容提供者方式(ContentProvider)进行存储的方法

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

白菜论坛,龙飘飘图片,尖锐湿疣的早期症状

本文实例讲述了android编程使用内容提供者方式进行存储的方法。分享给大家供大家参考,具体如下:

内容提供者(contentprovider)主要作用是对外共享数据,如果数据通过内容提供者对外共享了,那么其他应用就可以从内容提供者中查询到数据,并且可更新数据、删除数据、添加数据,如果采用文件的操作模式对外共享数据,数据的访问方式会因为存储方式的不同导致数据的访问方式无法得到统一,不同存储方式文件对外进行共享其访问的api是不一样的,如果采用内容提供者对外共享数据就会统一了数据的访问方式。采用统一的api访问共享的数据。

创建内容提供者步骤

1.创建内容提供者需要继承android.content.contentprovider

2.清单文件中进行配置:

<!--android:name:指定内容提供者的类名,android:authorities:调用内容..名称,随意取  -->
<provider android:name=".personprovider" android:authorities="cn.test.providers.personprovider"/>

contentprovider类主要方法

复制代码 代码如下:
public boolean oncreate()

该方法在contentprovider创建后就会被调用, android开机后, contentprovider在其它应用第一次访问它时才会被创建。
复制代码 代码如下:
public uriinsert(uri uri, contentvalues values)

该方法用于供外部应用往contentprovider添加数据。
复制代码 代码如下:
public int delete(uri uri, string selection,string[] selectionargs)

该方法用于供外部应用从contentprovider删除数据。
复制代码 代码如下:
public int update(uri uri, contentvalues values, stringselection, string[] selectionargs)

该方法用于供外部应用更新contentprovider中的数据。
复制代码 代码如下:
public cursorquery(uri uri, string[]projection, string selection, string[] selectionargs, string sortorder)

该方法用于供外部应用从contentprovider中获取数据。

示例:

内容提供者类,实现数据的增删改查

public class personprovider extends contentprovider {
  //创建工具类实现uri匹配
  private static final urimatcher matcher = new urimatcher(urimatcher.no_match);
  private static final int persons = 1;
  private static final int person = 2;
  static{
    matcher.adduri("cn.test.providers.personprovider", "person", persons);
    matcher.adduri("cn.test.providers.personprovider", "person/#", person);
  }
  private dbopenhelper dbopenhelper = null;
  @override
  public boolean oncreate() {
    dbopenhelper = new dbopenhelper(getcontext());
    return true;
  }
  @override
  public cursor query(uri uri, string[] projection, string selection,
      string[] selectionargs, string sortorder) {
    sqlitedatabase db = dbopenhelper.getreadabledatabase();
    switch (matcher.match(uri)) {
    case persons: // 获取person表中的所有数据  /person
      return db.query("person", projection, selection, selectionargs, null, null, sortorder);
    case person: // 获取person表中的指定id的数据 /person/20
      long id = contenturis.parseid(uri);
      string where = "personid="+ id;
      if(selection!=null && !"".equals(selection.trim())){
        where += " and "+ selection;
      }
      return db.query("person", projection, where, selectionargs, null, null, sortorder);
    default:
      throw new illegalargumentexception("unknown uri:"+ uri);
    }
  }
  @override
  public string gettype(uri uri) {
    // todo auto-generated method stub
    return null;
  }
  @override
  public uri insert(uri uri, contentvalues values) {
    //取得数据库操作实例
    sqlitedatabase db = dbopenhelper.getwritabledatabase();
    switch (matcher.match(uri)) {
    case persons:
      //执行添加,返回行号,如果主健字段是自增长的,那么行号会等于主键id
      long rowid = db.insert("person", "name", values);
      //得到拼好的uri
      uri inserturi = contenturis.withappendedid(uri, rowid);
      //发出数据变化通知(person表的数据发生变化)
      getcontext().getcontentresolver().notifychange(uri, null);
      return inserturi;
    default:
      //不能识别uri
      throw new illegalargumentexception("unknown uri:"+ uri);
    }
  }
  @override
  public int delete(uri uri, string selection, string[] selectionargs) {
    sqlitedatabase db = dbopenhelper.getwritabledatabase();
    //受影响的行数
    int num = 0;
    switch (matcher.match(uri)) {
    case persons: // 删除person表中的所有数据  /person
      num = db.delete("person", selection, selectionargs);
      break;
    case person: // 删除person表中的指定id的数据 /person/20
      long id = contenturis.parseid(uri);
      string where = "personid="+ id;
      if(selection!=null && !"".equals(selection.trim())){
        where += " and "+ selection;
      }
      num = db.delete("person", where, selectionargs);
      break;
    default:
      throw new illegalargumentexception("unknown uri:"+ uri);
    }
    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 persons: // 更新person表中的所有数据  /person
      num = db.update("person", values, selection, selectionargs);
      break;
    case person: // 更新person表中的指定id的数据 /person/20
      long id = contenturis.parseid(uri);
      string where = "personid="+ id;
      if(selection!=null && !"".equals(selection.trim())){
        where += " and "+ selection;
      }
      num = db.update("person", values, where, selectionargs);
      break;
    default:
      throw new illegalargumentexception("unknown uri:"+ uri);
    }
    return num;
  }
}

其他工程中访问:

public class accesscontentproidertest extends androidtestcase {
  public void testinsert() throws throwable{
    contentresolver resolver = getcontext().getcontentresolver();
    uri uri = uri.parse("content://cn.test.providers.personprovider/person");
    contentvalues values = new contentvalues();
    values.put("name", "lili");
    values.put("phone", "110");
    values.put("amount", "3000000000");
    resolver.insert(uri, values);
  }
  public void testdelete() throws throwable{
    contentresolver resolver = getcontext().getcontentresolver();
    uri uri = uri.parse("content://cn.test.providers.personprovider/person");
    int num =resolver.delete(uri, null, null);
  }
  public void testupdate() throws throwable{
    contentresolver resolver = getcontext().getcontentresolver();
    uri uri = uri.parse("content://cn.test.providers.personprovider/person/65");
    contentvalues values = new contentvalues();
    values.put("amount", 500);
    resolver.update(uri, values, null, null);
  }
  public void testquery() throws throwable{
    contentresolver resolver = getcontext().getcontentresolver();
    uri uri = uri.parse("content://cn.test.providers.personprovider/person/65");
    cursor cursor = resolver.query(uri, null, null, null, "personid asc");
    while(cursor.movetonext()){
      string name = cursor.getstring(cursor.getcolumnindex("name"));
      log.i("accesscontentprovidertest", name);
    }
  }
}

希望本文所述对大家android程序设计有所帮助。

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

相关文章:

验证码:
移动技术网