ContentProvider基本使用

 manifest.xml修改,添加权限和规则

    
    

    
    

    
        

        
            
                
            

ContentProvider类

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

public class NameProvider extends ContentProvider {

    private String mTableName;
    private Context mContext;
    private SQLiteDatabase mDatabase;
    private static final UriMatcher mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    
    public static final String AUTHORITY = "com.cultraview.ctvmenu";
    public static final Uri URI_CONTENT = Uri.parse("content://" + AUTHORITY + "/sourcename");
    public static final Uri URI_EDITNAME = Uri.parse("content://" + AUTHORITY + "/sourcename/editName");
    public static final Uri URI_INDEX = Uri.parse("content://" + AUTHORITY + "/sourcename/source_id");

    //rule
    private static final int INSERT = 0;
    private static final int DELETE = 1;
    private static final int QUERY = 2;
    private static final int UPDATE = 3;
    

    private EditSourceNameDBHelper dbHelper;
    
    //写入主机名的匹配规则
    static {
        mUriMatcher.addURI(AUTHORITY, "sourcename/insert", INSERT);
        mUriMatcher.addURI(AUTHORITY, "sourcename/delete", DELETE);
        mUriMatcher.addURI(AUTHORITY, "sourcename/query", QUERY);
        mUriMatcher.addURI(AUTHORITY, "sourcename/update", UPDATE);
    }
    
    //对外提供的匹配规则
    public interface URI{
        Uri INSERT = Uri.parse("content://"+AUTHORITY+"/sourcename/insert");
        Uri DELETE = Uri.parse("content://"+AUTHORITY+"/sourcename/delete");
        Uri QUERY = Uri.parse("content://"+AUTHORITY+"/sourcename/query");
        Uri UPDATE = Uri.parse("content://"+AUTHORITY+"/sourcename/update");
    }

    @Override
    public boolean onCreate() {
        initProvider();
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
            String sortOrder) {
        if (mUriMatcher.match(uri) == QUERY) {
            Cursor cursor = mDatabase.query(mTableName, projection, selection, selectionArgs, null,null, sortOrder);
            return cursor;
        }else {
            throw new IllegalAccessError("unKnow Uri,query="+uri);
        }
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        if (mUriMatcher.match(uri) == INSERT) {
            long id = mDatabase.insert("sourcename", null, values);
            uri = ContentUris.withAppendedId(URI_CONTENT, id);
            return uri;
        }else {
            throw new IllegalAccessError("unKnow Uri,Insert="+uri);
        }
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        if (mUriMatcher.match(uri) == DELETE) {
            int deleteCount = mDatabase.delete(mTableName, selection, selectionArgs);
            return deleteCount;
        }else {
            throw new IllegalAccessError("unKnow Uri,delete="+uri);
        }
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        if (mUriMatcher.match(uri) == UPDATE) {
            int updateCount = mDatabase.update(mTableName, values, selection, selectionArgs);
            return updateCount;
        }else {
            throw new IllegalAccessError("unKnow Uri,update="+uri);
        }
    }

    @Override
    public String getType(Uri arg0) {
        return null;
    }

    private void initProvider() {
        mTableName = EditSourceNameDBHelper.TABLE_NAME;
        mContext = getContext();
        dbHelper = new EditSourceNameDBHelper(mContext);
        mDatabase = dbHelper.getWritableDatabase();
    }
}

 使用时,

ContentResolver resolver = mContext.getContentResolver();

通过resolver调用ContentProvider类中的方法进行读写数据

你可能感兴趣的:(功能实现)