首页 > 移动平台 > 详细

[android] 内容提供者实现

时间:2016-03-13 00:48:51      阅读:201      评论:0      收藏:0      [点我收藏+]

上一节的主机名类似网络上的域名,协议是content://,可以定义一下规则

content://主机名/insert 添加操作

content://主机名/delete 删除操作

content://主机名/query 查询操作

content://主机名/update 修改操作

规则定义好之后,我们需要解析一下这个字符串,系统提供了一个api来匹配这个字符串

UriMatcher类,new出来对象new UriMatcher(code) code是个int,表示未匹配到的返回值,一般使用常量UriMatcher.NO_MATCH-1,把他定义成静态成员属性

 

定义一个静态代码块来测试一下这个matcherstatic{},调用UriMatcher对象的addURI(authorities,path,code)方法,参数authoritiesString主机名,pathString操作名,codeint匹配成功的返回值一般定义成常量private static final int增删查改四个常量。这个就是添加一组匹配规则

 

实现的主要方法

onCreate()方法

当内容提供者这个类开启的时候回调此方法,初始化数据库帮助对象如PersonSQLiteHelper

 

query(uri,projection,selection,selectArgs,sortOrder) (参数:Uri对象别人传过来的uri,字段,条件,条件对应的参数,排序)

方法里面调用matcher对象的match(uri)方法,对所传的uri进行匹配,如果成功就返回上面定义的匹配码,匹配成功调用helper对象的getReadableDatabase()方法获取数据库对象,调用db对象的query(table,columns,selection,selectionArgs,groupby,having,orderBy)方法得到Cursor结果集对象,细节db不要close了,框架会自动关闭

 

getType(uri),返回这个urimime类型

 

测试这个ContentProvider,新建一个应用,得到手机的中间人,通过getContentResolver()方法获取ContentResolver对象

调用ContentResolver对象的query(uri,projection,selection,selectArgs,sortOrder)方法,(参数:Uri对象别人传过来的uri,字段,条件,条件对应的参数,排序),返回Cursor对象。

获取Uri对象,通过Uri类的parse(uriString)方法,参数:content://主机名/操作名

While循环Cursor对象。

测试报错权限问题 清单文件加这个 android:exported="true"

 

ddms面板上左侧的进程列表,点击上面工具栏的stop按钮,关闭进程,当我调用ContentProvider的时候,进程会再次开启。

 

内容提供者:

 

package com.tsh.database;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

public class PersonDbProvider extends ContentProvider {
    private static final int INSERT = 1;
    private static final int SELECT = 2;
    private static final int DELETE = 3;
    private static final int UPDATE = 4;
    private static UriMatcher matcher=new UriMatcher(UriMatcher.NO_MATCH);
    private PersonSQLiteOpenHelper helper;
    static{
        //增加一组规则
        matcher.addURI("com.tsh.database.personprovider", "insert", INSERT);
        matcher.addURI("com.tsh.database.personprovider", "select", SELECT);
        matcher.addURI("com.tsh.database.personprovider", "delete", DELETE);
        matcher.addURI("com.tsh.database.personprovider", "update", UPDATE);
    }
    //初始化helper
    @Override
    public boolean onCreate() {
        helper=new PersonSQLiteOpenHelper(getContext());
        return false;
    }
    //查询
    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        //验证
        if(matcher.match(uri)==SELECT){
            SQLiteDatabase db=helper.getReadableDatabase();
            Cursor cursor=db.query("person", projection, selection, selectionArgs, null, null, sortOrder);
            return cursor;
        }
        return null;
    }

    @Override
    public String getType(Uri uri) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
            String[] selectionArgs) {
        // TODO Auto-generated method stub
        return 0;
    }

}

 

测试:

        ContentResolver resolver=getContentResolver();
        Uri uri=Uri.parse("content://com.tsh.database.personprovider/select");
        Cursor cursor=resolver.query(uri, null, null, null, null);
        while(cursor.moveToNext()){
            String name=cursor.getString(cursor.getColumnIndex("name"));
            System.out.println("name"+name);
        }
        cursor.close();

 

[android] 内容提供者实现

原文:http://www.cnblogs.com/taoshihan/p/5270903.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!