博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 下ContentProvider的学习
阅读量:6406 次
发布时间:2019-06-23

本文共 4462 字,大约阅读时间需要 14 分钟。

hot3.png

使用contentProvider 

1、要先继承contentProvider

2、要在配置文件中声明contentProvider

 <permission android:name="aa.bb.cc.read" ></permission>

    <permission android:name="aa.bb.cc.write" ></permission>

 <provider

            android:name=".providers.PersonContentProvider"

            android:authorities="com.itheima28.sqlitedemo.providers.PersonContentProvider"

            android:readPermission="aa.bb.cc.read"

            android:writePermission="aa.bb.cc.write" >

        </provider>

import android.content.ContentProvider;

import android.content.ContentUris;

import android.content.ContentValues;

import android.content.UriMatcher;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.net.Uri;

public class PersonContentProvider extends ContentProvider {

private static final String AUTHORITY = "com.itheima28.sqlitedemo.providers.PersonContentProvider";

private static final int PRESON_INSERT_CODE = 0;// 操作person表添加的操作的uri匹配码

private static final int PERSON_DELETE_CODE = 1;

private static final int PERSON_UPDATE_CODE = 2;

private static final int PERSON_QUERY_ALL_CODE = 3;

private static final int PERSON_QUERY_ITEM_CODE = 4;

private static UriMatcher uriMatcher;

private PersonSQLiteOpenHelper mOpenHelper;// person表的数据库帮助对象

static {

uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

// 添加一些uri(分机号)

// content://com.itheima28.sqlitedemo.providers.PersonContentProvider/person/insert

uriMatcher.addURI(AUTHORITY, "person/insert", PRESON_INSERT_CODE);

// content://com.itheima28.sqlitedemo.providers.PersonContentProvider/person/delete

uriMatcher.addURI(AUTHORITY, "person/delete", PERSON_DELETE_CODE);

// content://com.itheima28.sqlitedemo.providers.PersonContentProvider/person/update

uriMatcher.addURI(AUTHORITY, "person/update", PERSON_UPDATE_CODE);

// content://com.itheima28.sqlitedemo.providers.PersonContentProvider/person/queryAll

uriMatcher.addURI(AUTHORITY, "person/queryAll", PERSON_QUERY_ALL_CODE);

// content://com.itheima28.sqlitedemo.providers.PersonContentProvider/person/query/#

uriMatcher.addURI(AUTHORITY, "person/query/#", PERSON_QUERY_ITEM_CODE);

}

@Override

public boolean onCreate() {

mOpenHelper = new PersonSQLiteOpenHelper(getContext());

return true;

}

@Override

public Cursor query(Uri uri, String[] projection, String selection,

String[] selectionArgs, String sortOrder) {

SQLiteDatabase db = mOpenHelper.getReadableDatabase();

switch (uriMatcher.match(uri)) {

case PERSON_QUERY_ALL_CODE:  // 查询所有人的uri

if(db.isOpen()) {

Cursor cursor = db.query("person", projection, selection, selectionArgs, null, null, sortOrder);

return cursor;

// db.close(); 返回cursor结果集时, 不可以关闭数据库

}

break;

case PERSON_QUERY_ITEM_CODE:// 查询的是单条数据, uri末尾出有一个id

if(db.isOpen()) {

long id = ContentUris.parseId(uri);

Cursor cursor = db.query("person", projection, "_id = ?", new String[]{id + ""}, null, null, sortOrder);

return cursor;

}

break;

default:

throw new IllegalArgumentException("uri不匹配: " + uri);

}

return null;

}

@Override

public String getType(Uri uri) {

switch (uriMatcher.match(uri)) {

case PERSON_QUERY_ALL_CODE: // 返回多条的MIME-type

return "vnd.android.cursor.dir/person";

case PERSON_QUERY_ITEM_CODE: // 返回单条的MIME-TYPE

return "vnd.android.cursor.item/person";

default:

break;

}

return null;

}

@Override

public Uri insert(Uri uri, ContentValues values) {

switch (uriMatcher.match(uri)) {

case PRESON_INSERT_CODE:// 添加人到person表中

SQLiteDatabase db = mOpenHelper.getWritableDatabase();

if(db.isOpen()) {

long id = db.insert("person", null, values);

db.close();

return ContentUris.withAppendedId(uri, id);

}

break;

default:

throw new IllegalArgumentException("uri不匹配: " + uri);

}

return null;

}

@Override

public int delete(Uri uri, String selection, String[] selectionArgs) {

switch (uriMatcher.match(uri)) {

case PERSON_DELETE_CODE:// 在person表中删除数据的操作

SQLiteDatabase db = mOpenHelper.getWritableDatabase();

if(db.isOpen()) {

int count = db.delete("person", selection, selectionArgs);

db.close();

return count;

}

break;

default:

throw new IllegalArgumentException("uri不匹配: " + uri);

}

return 0;

}

@Override

public int update(Uri uri, ContentValues values, String selection,

String[] selectionArgs) {

switch (uriMatcher.match(uri)) {

case PERSON_UPDATE_CODE: // 更新person表的操作

SQLiteDatabase db = mOpenHelper.getWritableDatabase();

if(db.isOpen()) {

int count = db.update("person", values, selection, selectionArgs);

db.close();

return count;

}

break;

default:

throw new IllegalArgumentException("uri不匹配: " + uri);

}

return 0;

}

}

转载于:https://my.oschina.net/zaizaiangels/blog/514397

你可能感兴趣的文章
深入Weex系列(十)Weex SDK可优化细节思考
查看>>
Nginx的upstream_response_time
查看>>
Linux 探索之旅 | 第五部分第八课:用 Shell 做统计练习
查看>>
回顾2017系列篇(五):人工智能给UI/UX设计带来的影响
查看>>
两种折线平滑方案
查看>>
你真的理解 getLocationInWindow 了吗?
查看>>
webpack loader—自己写一个按需加载插件
查看>>
一起读经典《C Primer Plus(第6版)中文版》
查看>>
带你玩转小程序开发实践|含直播回顾视频
查看>>
CentOS 安装 Jenkins
查看>>
Redis入门第五篇【存储数据结构之set类型】
查看>>
Istio 技术与实践01: 源码解析之 Pilot 多云平台服务发现机制
查看>>
「深度剖析」程序员因为奇葩需求暴打pm,然后被双双开除
查看>>
“金九银十”,产品经理面试准备手册
查看>>
用R语言进行文本挖掘和主题建模
查看>>
CVE-2016-3714 - ImageMagick 命令执行分析
查看>>
企业安全实践经验分享
查看>>
数据结构笔记——总述
查看>>
iOS开发笔记(一):内存管理
查看>>
DRDS到MaxCompute(原ODPS)数据归档性能优化测试
查看>>