Android中ContentProvider的簡單使用

qingyezhu發表於2014-11-16

1、新建繼承ContentProvider的類

package com.wangzhu.demo;

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

public class MyContentProvider extends ContentProvider {

    /**
     * ContentProvider的資料訪問路徑
     */
    public static final Uri URI = Uri.parse("content://com.wangzhu.provider");

    private SQLiteDatabase database;

    @Override
    public int delete(Uri arg0, String arg1, String[] arg2) {
        return 0;
    }

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

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        database.insert("tab", "_id", values);
        return null;
    }

    @Override
    public boolean onCreate() {
        database = getContext().openOrCreateDatabase("myProvider.db3",
                Context.MODE_PRIVATE, null);
        database.execSQL("create table IF NOT EXISTS tab(_id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT NOT NULL)");
        return true;
    }

    @Override
    public Cursor query(Uri arg0, String[] arg1, String arg2, String[] arg3,
            String arg4) {
        Cursor cursor = database.query("tab", null, null, null, null, null,
                null);
        return cursor;
    }

    @Override
    public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
        return 0;
    }

}

 

 

2、在AndroidManifest.xml中為應用程式新增ContentProvider的訪問許可權

        <!-- android:exported設定為true,表示其他應用可以訪問,否則不可以訪問 -->
        <provider
            android:name="MyContentProvider"
            android:authorities="com.wangzhu.provider"
            android:exported="true" >
        </provider>

3、進行插入與查詢

 

        final Uri uri = MyContentProvider.URI;

    /**
     * 往Provider中寫入資料
     */
    protected void write() {
        //使用getContentResolver()方法獲取ContentResolver物件
        ContentResolver resolver = getContentResolver();
        ContentValues values = null;

        values = new ContentValues();
        values.put("name", "Java");
        //呼叫ContentResolver物件的insert方法,插入資料
        resolver.insert(uri, values);

        values = new ContentValues();
        values.put("name", "Swift");
        resolver.insert(uri, values);

        values = new ContentValues();
        values.put("name", "Python");
        resolver.insert(uri, values);

        values = new ContentValues();
        values.put("name", "C#");
        resolver.insert(uri, values);

    }

    /**
     * 讀取Provider中的內容
     */
    protected void read() {
        StringBuilder accum = new StringBuilder();

        //使用getContentResolver()方法獲取ContentResolver物件
        //呼叫ContentResolver物件的query方法,查詢資料,返回Cursor物件
        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        cursor.moveToFirst();
        for (int i = 0, count = cursor.getCount(); i < count; i++) {
            accum.append(cursor.getString(cursor.getColumnIndex("name")))
                    .append("\n");
            cursor.moveToNext();
        }
        System.err.println("read: " + accum);
    }

 

相關文章