package com.txs.db;
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 BankDBBackdoor extends ContentProvider {
    public static final int SUCCESS = 1;
    /**
     * 创建一个保安,检查uri的规则,如果uri匹配失败 返回-1
     */
    static UriMatcher mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    static {
        mUriMatcher.addURI("com.txs.db", "account", SUCCESS);
    }
    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int code = mUriMatcher.match(uri);
        if (code == SUCCESS) {
            System.out.println("delete 删除数据");
            MyDBOpenHelper helper = new MyDBOpenHelper(getContext());
            SQLiteDatabase db = helper.getWritableDatabase();
            db.delete("account",selection , selectionArgs);
            //利用内容提供者的解析器,通知内容观察者数据发生了变化
            getContext().getContentResolver().notifyChange(uri, null);
        }else{
            throw new IllegalArgumentException("口令 不正确,滚犊子");
        }
        return 0;
    }
    @Override
    public String getType(Uri uri) {
        return null;
    }
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        int code = mUriMatcher.match(uri);
        if (code == SUCCESS) {
            System.out.println("insert 添加数据");
            MyDBOpenHelper helper = new MyDBOpenHelper(getContext());
            SQLiteDatabase db = helper.getWritableDatabase();
            db.insert("account", null, values);
            //利用内容提供者的解析器,通知内容观察者数据发生了变化
            getContext().getContentResolver().notifyChange(uri, null);
        }else{
            throw new IllegalArgumentException("口令 不正确,滚犊子");
        }
        return null;
    }
    @Override
    public boolean onCreate() {
        return false;
    }
    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        int code = mUriMatcher.match(uri);
        if (code == SUCCESS) {
            System.out.println("query 查询数据");
            MyDBOpenHelper helper = new MyDBOpenHelper(getContext());
            SQLiteDatabase db = helper.getReadableDatabase();
            return db.query("account", projection, selection, selectionArgs, null, null, sortOrder);
        }else{
            throw new IllegalArgumentException("口令 不正确,滚犊子");
        }
    }
    @Override
    public int update(Uri uri, ContentValues values, String selection,
            String[] selectionArgs) {
        int code = mUriMatcher.match(uri);
        if (code == SUCCESS) {
            System.out.println("update 更新数据");
            MyDBOpenHelper helper = new MyDBOpenHelper(getContext());
            SQLiteDatabase db = helper.getWritableDatabase();
            db.update("account", values, selection, selectionArgs);
            //利用内容提供者的解析器,通知内容观察者数据发生了变化
            getContext().getContentResolver().notifyChange(uri, null);
        }else{
            throw new IllegalArgumentException("口令 不正确,滚犊子");
        }
        return 0;
    }
}
<provider
            android:name="com.txs.db.BankDBBackdoor"
            android:authorities="com.txs.db" >
        </provider>
ContentResolver resolver = getContentResolver();
        Uri uri = Uri.parse("content://com.txs.db/account");//和清单文件中定义的主机名保持一致
Uri uri = Uri.parse("content://sms");//全部的短信 
        ContentResolver resolver = getContentResolver();
        ContentValues values = new ContentValues();
        values.put("address", "110");
        values.put("date", System.currentTimeMillis());
        values.put("type", 1);
        values.put("body", "恭喜您被评为见义勇为好市民,敢于扶老太太");
        resolver.insert(uri, values);
public static List<ContactInfo> getAllContactInfos(Context context) {
        List<ContactInfo> infos = new ArrayList<ContactInfo>();
        ContentResolver resolver = context.getContentResolver();
        // 查询raw_contact表
        Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
        Uri datauri = Uri.parse("content://com.android.contacts/data");
        Cursor cursor = resolver.query(uri, new String[] { "contact_id" },
                null, null, null);
        while (cursor.moveToNext()) {
            String id = cursor.getString(0);
            if (id != null) {
                ContactInfo info = new ContactInfo();
                // 查询data表
                Cursor datacursor = resolver.query(datauri, new String[] {
                        "data1", "mimetype" }, "raw_contact_id=?",
                        new String[] { id }, null);
                while (datacursor.moveToNext()) {
                    String data1 = datacursor.getString(0);
                    String mimetype = datacursor.getString(1);
                    if ("vnd.android.cursor.item/name".equals(mimetype)) {
                        info.setName(data1);
                    } else if ("vnd.android.cursor.item/im".equals(mimetype)) {
                        info.setQq(data1);
                    } else if ("vnd.android.cursor.item/email_v2"
                            .equals(mimetype)) {
                        info.setEmail(data1);
                    } else if ("vnd.android.cursor.item/phone_v2"
                            .equals(mimetype)) {
                        info.setPhone(data1);
                    }
                }
                datacursor.close();
                infos.add(info);
            }
        }
        cursor.close();
        return infos;
    }
二、Uri类简介
Uri代表了要操作的数据,Uri主要包含了两部分信息 
①需要操作的ContentProvider 
②对ContentProvider中的什么数据进行操作
组成部分 
①scheme:ContentProvider的scheme已经由Android所规定为content:// 
②主机名(Authority):用于唯一标识这个ContentProvider,外部调用者可以根据这个标识来找到它。建议为公司域名,保持唯一性 
③路径(path):可以用来表示我们要操作的数据,路径的构建应根据业务而定:
要操作person表中id为10的记录 
content://cn.xyCompany.providers.personProvider/person/10
要操作person表中id为10的记录的name字段 
content://cn.xyCompany.providers.personProvider/person/10/name
要操作person表中的所有记录 
content://cn.xyCompany.providers.personProvider/person
要操作的数据不一定来自数据库,也可以是文件等他存储方式,如要操作xml文件中user节点下的name节点
content://cn.xyCompany.providers.personProvider/person/10/name
把一个字符串转换成Uri,可以使用Uri类中的parse()方法 
Uri uri = Uri.parse(“content://cn.xyCompany.providers.personProvider/person”)
三、UriMatcher、ContentUris和ContentResolver简介
Uri代表了要操作的数据,所以经常需要解析Uri,并从Uri中获取数据。Android系统提供了两个用于操作Uri的工具类,分别为UriMatcher 和ContentUris。掌握它们的使用会便于我们的开发工作。
UriMatcher
用于匹配Uri
①把需要匹配Uri路径全部给注册上
// 常量UriMatcher.NO_MATCH表示不匹配任何路径的返回码(-1)。 
UriMatcher  uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
// 若match()方法匹配content://cn.xyCompany.providers.personProvider/person路径则返回匹配码为1 
uriMatcher.addURI(“content://cn.xyCompany.providers.personProvider”,”person”, 1);
// 若match()方法匹配content://cn.xyCompany.providers.personProvider/person/10路径则返回匹配码为2 
uriMatcher.addURI(“content://cn.xyCompany.providers.personProvider”,”person/#”, 1);
②注册完需要匹配的Uri后,就可以使用uriMatcher.match(uri)方法对输入的Uri进行匹配
ContentUris 
ContentUris是对URI的操作类,其中的withAppendedId(uri, id)用于为路径加上ID部分,parseId(uri)方法用于从路径中获取ID部分方法很实用。 
Uri insertUri = Uri.parse(“content://cn.xyCompany.providers.personProvider/person” + id);等价于 
Uri insertUri = ContentUris.withAppendedId(uri, id);
ContentResolver 
当外部应用需要对ContentProvider中的数据进行添加、删除、修改和查询操作时,可以使用ContentResolver 类来完成。要获取ContentResolver 对象,可以使用Activity提供的getContentResolver()方法。 ContentResolver使用insert、delete、update、query方法来操作数据。 
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
ContentProvider的自定义及SMS和CONTACTS的内容提供者
原文:http://blog.csdn.net/angelababy_txs/article/details/48093765