内容提供者

   <!-- 定义权限 -->
    <permission 
        android:name="aaa.bbb.ccc"
        ></permission>
    <permission 
        android:name="aaa.bbb.ccc.ddd"
    ></permission>  


 <!-- 写contentpriverder 必须要在主xml文件中申明 -->
        <provider 
            android:name="com.xh.tx.contentprivoder.PersonContentPrivoder"
            android:authorities="com.xh.tx.contentprivoder.PersonContentPrivoder"
            android:readPermission="aaa.bbb.ccc"
            android:writePermission="aaa.bbb.ccc.ddd"
            ></provider>



public class PersonContentPrivoder extends ContentProvider

{
private MySQLLiteHelper helper = null;

//uriMatcher 工厂
private static UriMatcher uriMatcher = null;
private static final String AURHORITH = "com.xh.tx.contentprivoder.PersonContentPrivoder"; // 这个就是主号码


private static final int PERSON_INSERT_CODE = 0;
private static final int PERSON_UPDATE_CODE = 1;
private static final int PERSON_DELETE_CODE = 2;
private static final int PERSON_QUERY_CODE = 3;

static{
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);//参数就是分机号

//添加插入的分机号
//访问地址:content://com.xh.tx.contentprivoder.PersonContentPrivoder/person/insert

uriMatcher.addURI(AURHORITH, "person/insert", PERSON_INSERT_CODE);

//更新插入的分机号
//访问地址:content://com.xh.tx.contentprivoder.PersonContentPrivoder/person/update

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

//删除的分机号
//访问地址:content://com.xh.tx.contentprivoder.PersonContentPrivoder/person/delete

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

//查询的分机号
//访问地址:content://com.xh.tx.contentprivoder.PersonContentPrivoder/person/query

uriMatcher.addURI(AURHORITH, "person/query", PERSON_QUERY_CODE);
}




//在内容提供者创建的时候初始化我们的数据库工具类
@Override
public boolean onCreate() {

helper = new MySQLLiteHelper(getContext(), null, null, -1);

return false;
}


@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {

switch (uriMatcher.match(uri)) {
case PERSON_QUERY_CODE:

SQLiteDatabase db = helper.getWritableDatabase();

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

// db.close(); // 不能关掉数据库否者访问不到数据

return cursor;
}

break;


default:
throw new IllegalArgumentException("参数错误 :" + uri); //匹配不上的时候抛出异常提醒uri传递错误
}
return null;
}


//由系统调用,所以我们一般不会重写他
@Override
public String getType(Uri uri) {
return null;
}


//inert方法提供真实的服务
@Override
public Uri insert(Uri uri, ContentValues values) {
//访问地址:content://com.xh.tx.contentprivoder.PersonContentPrivoder/person/insert
//返回一个uri类型的数据 一般我们就将就访问地址,然后在地址后面追加添加成功以后返回的id


switch (uriMatcher.match(uri)) {
case PERSON_INSERT_CODE:

SQLiteDatabase db = helper.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:

SQLiteDatabase db = helper.getWritableDatabase();

if(db.isOpen())
{
int id = db.delete("person", selection, selectionArgs);
db.close();

return id;
}
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:

SQLiteDatabase db = helper.getWritableDatabase();

if(db.isOpen())
{
int id = db.update("person", values, selection, selectionArgs);
db.close();

return id;
}
break;
default:
throw new IllegalArgumentException("参数错误 :" + uri); //匹配不上的时候抛出异常提醒uri传递错误
}
return 0;
}


}

你可能感兴趣的:(内容提供者)