Android自定义内容提供者使用

来源:互联网 发布:视频讲课软件 编辑:程序博客网 时间:2024/06/03 08:52
    在android 中,我们们常常会使用到内部数据库,同时又希望自己的部分数据可以被其他应用程序访问到,我们可以通过自定义内容提供者来实现这个功能。想访问到contentProvider,提供的数据需要两个条件一是 程序的uri,二是获得contentResolver 对象,getContentResolver()所调用的方法其实是调用的 contentProvider 内部实现的方法 。
/** * Description: 把当前应用下的私有数据库 提供给外界<br/> * Copyright (c) , 2016, Jansonxu <br/> * This program is protected by copyright laws. <br/> * Program Name:UserContentProvider.java <br/> * Date: 2016年3月9日 *  * @author 李阳 * @version : 1.0 */package com.example.contentprovider.provider;import com.example.contentprovider.util.Dbhelper;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 UserContentProvider extends ContentProvider {    //① 定义该 ContentProvider 组件的唯一标识  通常用包名 + 数据库名来命名 必须小写(保证唯一。包名是唯一的)    // 包名在清单上边 就能看到    public static final String AUTHORITY = "com.example.contentprovider.user_1";    //② 为该组件中可以被其他应用访问的表 定义 CODE标识    public static final int CODE_USER = 1;    public static final int CODE_ORDER = 2;    //③ 定义UriMatcher 对象  之前有用到过      public static UriMatcher uriMatcher;  //用于生成uri    //静态代码块 随着类的加载而执行     static{        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);//        //为 t_user 表生成Uri  content://com.example.contentprovider.user_1/user        uriMatcher.addURI(AUTHORITY, "user", CODE_USER); //path 自己随便定的        uriMatcher.addURI(AUTHORITY, "order", CODE_ORDER); //path 自己随便定的    }    /*      *      */    private Dbhelper dbHelper;    @Override    public boolean onCreate() {        // 初始化        dbHelper = new Dbhelper(getContext());        return false;    }    /* (non-Javadoc)     * @see android.content.ContentProvider#query(android.net.Uri, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String)     */    @Override    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {        int code =  uriMatcher.match(uri);// 返回的是 uri 对应的code 标识        Cursor cursor = null;        SQLiteDatabase db = dbHelper.getReadableDatabase();        switch (code) {        case CODE_USER:            //有问题  这个 t_user 是表名的话 前边 那个建立对应关系  User 是什么             cursor = db.query("t_user", projection, selection, selectionArgs, null, null, sortOrder);            break;        case CODE_ORDER:            break;        default:            break;        }        return cursor;    }    /* (non-Javadoc)     * @see android.content.ContentProvider#getType(android.net.Uri)     */    @Override    public String getType(Uri uri) {        // TODO Auto-generated method stub        return null;    }    /* (non-Javadoc)     * @see android.content.ContentProvider#insert(android.net.Uri, android.content.ContentValues)     */    @Override    public Uri insert(Uri uri, ContentValues values) {        SQLiteDatabase db = dbHelper.getWritableDatabase();        if(uriMatcher.match(uri)==UserContentProvider.CODE_USER){            long id = db.insert("t_user", null, values);              return ContentUris.withAppendedId(uri, id);        }        return null;    }    /* (non-Javadoc)     * @see android.content.ContentProvider#delete(android.net.Uri, java.lang.String, java.lang.String[])     */    @Override    public int delete(Uri uri, String selection, String[] selectionArgs) {        SQLiteDatabase db = dbHelper.getWritableDatabase();        int num = 0;        if(uriMatcher.match(uri)==UserContentProvider.CODE_USER){            num = db.delete("t_user", selection, selectionArgs);        }        return num;    }    /* (non-Javadoc)     * @see android.content.ContentProvider#update(android.net.Uri, android.content.ContentValues, java.lang.String, java.lang.String[])     */    @Override    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {        SQLiteDatabase db = dbHelper.getWritableDatabase();        if(uriMatcher.match(uri)==UserContentProvider.CODE_USER){            db.update("t_user", values, selection, selectionArgs);         }        return 0;    }}
0 0
原创粉丝点击