美文网首页
Android四大应用组件之ContentProvider篇

Android四大应用组件之ContentProvider篇

作者: flynnny | 来源:发表于2021-03-02 11:08 被阅读0次

总结自Android四大应用组件之ContentProvider篇
Android四大应用组件之Activity篇https://www.jianshu.com/p/d22981a8ad0e
Android四大应用组件之Service篇https://www.jianshu.com/p/f7d57803b86e
Android四大应用组件之ContentProvider篇https://www.jianshu.com/p/5f783f3db22d
Android四大应用组件之BroadcastReceiver篇https://www.jianshu.com/p/e6283dec21c7

ContentProvider+ContentResolver(主要修改这个)

理论概述

1.png 2.png 3.png 4.png 5.png

项目练习

contentprovider:(被调用方、数据所在方)
先建个表

public class DBHelper extends SQLiteOpenHelper{
//  public DBHelper(Context context,String name,CursorFactory factory,int version){
//    super(context,name,factory,version);
//  }
  public DBHelper(Context context){
    super(context,"zzz.db",null,1);
  }

  public void onCreate(SQLiteDatabase db){
    //建表
    db.execSQL("create table person(_id integer primary key autoincrement,name varchar)");

    db.execSQL("insert into person (name) values ('Tom')");
  }
  public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
    
  }
}

编写ContentProvider子类

class StudentContentProvider extends ContentProvider{
  //用来存放所有合法uri的容器
  private static UriMatcher matcher = new UriMatcher (UriMatcher.NO_MATCH);
  //保存合法uri
  //com.xxx.studentprovider是注册里的标识
  static{
    matcher.addURI("com.xxx.studentprovider","/person",1);
    matcher.addURI("com.xxx.studentprovider","/person/#",2);//#任意数字
  }
private DBHelper dehelper;

  public boolean onCreate(){
    dehelper = new DBHelper(getContext());
    return false;
  }

  //区别对待
  //content://com.xxx.studentprovider/person 不根据id查询
  //content://com.xxx.studentprovider/person/3 根据id查询
  public Cursor query(Uri uri,String[] projection,String selection,String[] selectionArgs,String sortOrder){
  
   //  得到连接对象
    SQLiteDatabase database = dehelper.getReadableDatabase();

    //匹配uri 返回code
    int code = matcher.match(uri);
    //合法 ---查询
    if(code==1){
      Cursor cursor= database.query("person",projection,selection,selectionArgs,null,null,nll);
      return cursor;
    }else if(code ==2){
      //得到id
      long id =ContentUris.parseId(uri);
      //查询
      Cursor cursor= database.query("person",projection,"_id=?",new String[]{id+""},null,null,nll);

      //这里不可以关闭db,关了之后cursor用不了了
      return cursor;
    }else{//不合法 抛出异常
      throw new RuntimeException("uri不合法");
    }
  }

  //content://com.xxx.studentprovider/person 不根据id插入
  //content://com.xxx.studentprovider/person/3 根据id插入(没有)
  public Uri insert (Uri uri,ContentValues values){
 //  得到连接对象
    SQLiteDatabase database = dehelper.getReadableDatabase();

    //匹配uri 返回code
    int code = matcher.match(uri);
    //合法 ---查询
    if(code==1){
      long id= database.insert("person",null,values);
      //将id加到uri中
      uri = ContentUris.withAppendedId(uri,id);
      //这里可以关闭db,应为是插入
      database.close();//应该在finally中关闭,这里简写。

      return uri;
    }else{//不合法 抛出异常
      database.close();//应该在finally中关闭,这里简写。
      throw new RuntimeException("uri不合法");
    }
  }

  //content://com.xxx.studentprovider/person 不根据id删除
  //content://com.xxx.studentprovider/person/3 根据id删除
  public int delete(Uri uri,String selection,String[] selectionArgs){
//  得到连接对象
    SQLiteDatabase database = dehelper.getReadableDatabase();

    //匹配uri 返回code
    int code = matcher.match(uri);
    //合法 ---查询
    int deleteCount =-1;
    if(code==1){
      deleteCount = database.delete("person",selection,selectionArgs);
    }else if(code ==2){
      //得到id
      long id =ContentUris.parseId(uri);
      //删除
      deleteCount = database.delete("person","_id="+id,null);

    }else{//不合法 抛出异常
      database.close();
      throw new RuntimeException("uri不合法");
    }
    database.close();
    return deleteCount ;
  }

  //content://com.xxx.studentprovider/person 不根据id更新
  //content://com.xxx.studentprovider/person/3 根据id更新
  public int update(Uri uri,ContentValues values,String selection,String[] selectionArgs){
//  得到连接对象
    SQLiteDatabase database = dehelper.getReadableDatabase();

    //匹配uri 返回code
    int code = matcher.match(uri);
    //合法 ---查询
    int updateCount =-1;
    if(code==1){
      updateCount = database.update("person",values,selection,selectionArgs);
    }else if(code ==2){
      //得到id
      long id =ContentUris.parseId(uri);
      //删除
      updateCount = database.update("person",values,"_id="+id,null);

    }else{//不合法 抛出异常
      database.close();
      throw new RuntimeException("uri不合法");
    }
    database.close();
    return updateCount ;
  }
  public String getType(Uri uri){
    return null;
  }
}

在manifest.xml中注册

<provider
  android:name=".StudentContentProvider"
  android:authorities="com.xxx.studentprovider"//标识名称,一般为全类名小写
  android:exported="true"//别的应用访问开关
/>

一旦注册上就会创建好 、初始化。

MainActivity里的contentresolver:

public void query(){
  //得到contentresolver对象
  ContentResolver resolver =getContentResolver();
  //调用query 得到cursor
  Uri uri = Uri.parse("content://com.xxx.studentprovider/person/");
  Cursor cursor=resolver.query(uri,null,null,null,null);
  //取出cursor中的数据
  while(cursor.moveToNext()){
    int id = cursor.getInt(0);
    String name = cursor.getString(1);
    Toast.makeText(this,id+":"+name,1).show();
  }
  cursor.close();
}

public void insert(){
  //得到contentresolver对象
  ContentResolver resolver =getContentResolver();
  //调用insert
  Uri uri = Uri.parse("content://com.xxx.studentprovider/person/");
  ContentValues.put("name","JACK");
  uri = resolver.insert(uri,values);
  
  Toast.makeText(this,uri.toString(),0).show();
}

public void update(){
  //得到contentresolver对象
  ContentResolver resolver =getContentResolver();
  //调用update
  Uri uri = Uri.parse("content://com.xxx.studentprovider/person/2");
  ContentValues.put("name","JACK2");
  int updateCount = resolver.update(uri,values,null,null);
  
  Toast.makeText(this,"updateCount ="+updateCount ,1).show();
}

public void delete(){
  //得到contentresolver对象
  ContentResolver resolver =getContentResolver();
  //调用update
  Uri uri = Uri.parse("content://com.xxx.studentprovider/person/2");
  ContentValues.put("name","JACK2");
  int deleteCount = resolver.delete(uri,null,null);
  
  Toast.makeText(this,"deleteCount ="+deleteCount ,1).show();
}

相关文章

网友评论

      本文标题:Android四大应用组件之ContentProvider篇

      本文链接:https://www.haomeiwen.com/subject/dafzfltx.html