赞
踩
第一步需要继承ContentProvider并重写下面方法:
public class PersonContentProvider extends ContentProvider{
public boolean onCreate()
public Uri insert(Uri uri, ContentValues values)
public int delete(Uri uri, String selection, String[] selectionArgs)
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs)
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
public String getType(Uri uri)}
第二步需要在AndroidManifest.xml使用<provider>对该ContentProvider进行配置,为了能让其他应用找到该ContentProvider , ContentProvider 采用了authorities(主机名/域名)对它进行唯一标识,你可以把 ContentProvider看作是一个网站(想想,网站也是提供数据者),authorities 就是他的域名:
<manifest .... >
<application android:icon="@drawable/icon" android:label="@string/app_name">
<provider android:name=".PersonContentProvider" android:authorities="cn.itcast.providers.personprovider"/>
</application>
</manifest>
代码:
-
- package cn.itcast.db;
-
- import cn.itcast.service.DBOpenHelper;
- 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 PersonContentProvider extends ContentProvider {
- private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
- private static final int PERSONS = 1;
- private static final int PERSON = 2;
- private DBOpenHelper dbOpenHelper;
- static{
- matcher.addURI("cn.itcast.providers.personprovider", "person", PERSONS);
- matcher.addURI("cn.itcast.providers.personprovider", "person/#", PERSON);
- }
-
- @Override
- public boolean onCreate() {
- dbOpenHelper = new DBOpenHelper(this.getContext());
- return true;
- }
-
- @Override
- public int delete(Uri uri, String selection, String[] selectionArgs) {
- SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
- int num = 0 ;//已经删除的记录数量
- switch (matcher.match(uri)) {
- case PERSONS:
- num = db.delete("person", selection, selectionArgs);
- break;
- case PERSON:
- long id = ContentUris.parseId(uri);
- String where = "personid="+ id;
- if(selection!=null && !"".equals(selection)){ // personid=12 and name=?
- where = where + " and "+ selection;
- }
- num = db.delete("person", where, selectionArgs);
- break;
- default:
- throw new IllegalArgumentException("Unkown Uri:"+ uri);
- }
- getContext().getContentResolver().notifyChange(uri, null);
- return num;
- }
-
- @Override
- public String getType(Uri uri) {//返回当前操作的数据类型
- switch (matcher.match(uri)) {
- case PERSONS://操作的是集合类型数据
- return "vnd.android.cursor.dir/person";
- case PERSON:
- return "vnd.android.cursor.item/person";
- default:
- throw new IllegalArgumentException("Unkown Uri:"+ uri);
- }
- }
-
- @Override
- public Uri insert(Uri uri, ContentValues values) {
- SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
- long id = 0 ;
- switch (matcher.match(uri)) {
- case PERSONS:
- id = db.insert("person", "personid", values);//得到记录的id
- getContext().getContentResolver().notifyChange(uri, null);
- return ContentUris.withAppendedId(uri, id);//返回代表新增记录的Uri
- case PERSON:
- id = db.insert("person", "personid", values);//得到记录的id
- String strUri = uri.toString();
- Uri personUri = Uri.parse(strUri.substring(0, strUri.lastIndexOf("/")));
- getContext().getContentResolver().notifyChange(personUri, null);
- return ContentUris.withAppendedId(personUri, id);
- default:
- throw new IllegalArgumentException("Unkown Uri:"+ uri);
- }
- }
-
- @Override
- public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
- SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
- switch (matcher.match(uri)) {
- case PERSONS:
- return db.query("person", projection, selection, selectionArgs, null, null, sortOrder);
-
- case PERSON:
- long id = ContentUris.parseId(uri);
- String where = "personid="+ id;
- if(selection!=null && !"".equals(selection)){ // personid=12 and name=?
- where = where + " and "+ selection;
- }
- return db.query("person", projection, where, selectionArgs, null, null, sortOrder);
-
- default:
- throw new IllegalArgumentException("Unkown Uri:"+ uri);
- }
- }
-
- @Override
- public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
- SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
- int num = 0 ;//已经修改的记录数量
- switch (matcher.match(uri)) {
- case PERSONS:
- num = db.update("person", values, selection, selectionArgs);
- break;
- case PERSON:
- long id = ContentUris.parseId(uri);
- String where = "personid="+ id;
- if(selection!=null && !"".equals(selection)){
- where = where + " and "+ selection;
- }
- num = db.update("person", values, where, selectionArgs);
- break;
- default:
- throw new IllegalArgumentException("Unkown Uri:"+ uri);
- }
- getContext().getContentResolver().notifyChange(uri, null);//通知数据发生变化
- return num;
- }
-
- }
- //常量UriMatcher.NO_MATCH表示不匹配任何路径的返回码
- UriMatcher sMatcher = new UriMatcher(UriMatcher.NO_MATCH);
- //如果match()方法匹配content://cn.itcast.provider.personprovider/person路径,返回匹配码为1
- sMatcher.addURI(“cn.itcast.provider.personprovider”, “person”, 1);//添加需要匹配uri,如果匹配就会返回匹配码
- //如果match()方法匹配content://cn.itcast.provider.personprovider/person/230路径,返回匹配码为2
- sMatcher.addURI(“cn.itcast.provider.personprovider”, “person/#”, 2);//#号为通配符
- switch (sMatcher.match(Uri.parse("content://cn.itcast.provider.personprovider/person/10"))) {
- case 1
- break;
- case 2
- break;
- default://不匹配
- break;
- }
注册完需要匹配的Uri后,就可以使用sMatcher.match(uri)方法对输入的Uri进行匹配,如果匹配就返回匹配码,匹配码是调用addURI()方法传入的第三个参数,假设匹配content://cn.itcast.provider.personprovider/person路径,返回的匹配码为1
- public boolean onCreate()
- 该方法在ContentProvider创建后就会被调用, Android开机后, ContentProvider在其它应用第一次访问它时才会被创建。
- public Uri insert(Uri uri, ContentValues values)
- 该方法用于供外部应用往ContentProvider添加数据。
- public int delete(Uri uri, String selection, String[] selectionArgs)
- 该方法用于供外部应用从ContentProvider删除数据。
- public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs)
- 该方法用于供外部应用更新ContentProvider中的数据。
- public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
- 该方法用于供外部应用从ContentProvider中获取数据。
- public String getType(Uri uri)
- 该方法用于返回当前Url所代表数据的MIME类型。如果操作的数据属于集合类型,那么MIME类型字符串应该以vnd.android.cursor.dir/开头,例如:要得到所有person记录的Uri为content://cn.itcast.provider.personprovider/person,那么返回的MIME类型字符串应该为:“vnd.android.cursor.dir/person”。如果要操作的数据属于非集合类型数据,那么MIME类型字符串应该以vnd.android.cursor.item/开头,例如:得到id为10的person记录,Uri为content://cn.itcast.provider.personprovider/person/10,那么返回的MIME类型字符串应该为:“vnd.android.cursor.item/person”。
- public Uri insert(Uri uri, ContentValues values)
- 该方法用于往ContentProvider添加数据。
- public int delete(Uri uri, String selection, String[] selectionArgs)
- 该方法用于从ContentProvider删除数据。
- public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs)
- 该方法用于更新ContentProvider中的数据。
- public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
- 该方法用于从ContentProvider中获取数据。
这些方法的第一个参数为Uri,代表要操作的ContentProvider和对其中的什么数据进行操作,假设给定的是: Uri.parse(“content://cn.itcast.providers.personprovider/person/10”),那么将会对主机名为cn.itcast.providers.personprovider的ContentProvider进行操作,操作的数据为person表中id为10的记录。
- ContentResolver resolver = getContentResolver();
- Uri uri = Uri.parse("content://cn.itcast.provider.personprovider/person");
- //添加一条记录
- ContentValues values = new ContentValues();
- values.put("name", "itcast");
- values.put("age", 25);
- resolver.insert(uri, values);
- //获取person表中所有记录
- Cursor cursor = resolver.query(uri, null, null, null, "personid desc");
- while(cursor.moveToNext()){
- Log.i("ContentTest", "personid="+ cursor.getInt(0)+ ",name="+ cursor.getString(1));
- }
- //把id为1的记录的name字段值更改新为liming
- ContentValues updateValues = new ContentValues();
- updateValues.put("name", "liming");
- resolver.update(updateIdUri, updateValues, null, null);
- public class PersonContentProvider extends ContentProvider {
- public Uri insert(Uri uri, ContentValues values) {
- db.insert("person", "personid", values);
- getContext().getContentResolver().notifyChange(uri, null);
- }
- }
如果ContentProvider的访问者需要得到数据变化通知,必须使用ContentObserver对数据(数据采用uri描述)进行监听,当监听到数据变化通知时,系统就会调用ContentObserver的onChange()方法:
- getContentResolver().registerContentObserver(
- Uri.parse("content://cn.itcast.providers.personprovider/person"),
- true,
- new PersonObserver(new Handler()));
- //true表示只要发出通知的Uri以方法第一个参数开头都能被监听到,否侧监听uri必须与发出通知的uri完全匹配才能被监听到
- public class PersonObserver extends ContentObserver{
- public PersonObserver(Handler handler) {
- super(handler);
- }
- public void onChange(boolean selfChange) {
- //此处可以进行相应的业务处理
- }
- }
- public class ContactUtils {
-
- // 给调用者返回一个集合
- public static List<ContactInfo> getContacts(Context context) {
-
- List<ContactInfo> contactInfos = new ArrayList<ContactInfo>();
-
- // 1 查询raw_contacts 表 查询 contact_id 列
- // 定义 查询raw_contacts 表的 uri
- Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
- // 拿到内容提供者的解析器
- ContentResolver resolver = context.getContentResolver();
- Cursor cursor = resolver.query(uri, new String[] { "contact_id" },
- null, null, null);
- while (cursor.moveToNext()) {
- ContactInfo info = new ContactInfo();
- String id = cursor.getString(0);
- if (id != null) {
- System.out.println("raw_contact-id~~~~" + id);
- info.setId(id);
-
- // 2 根据这个id 去查询 data 表
- Uri datauri = Uri.parse("content://com.android.contacts/data"); // 定义
- // data表的uri
- // 查询实际上 是data表和 mimetype 表的视图 视图中 定义mimetype_id 叫 mimetype
- Cursor dataCursor = resolver.query(datauri, new String[] {
- "mimetype", "data1" }, "raw_contact_id=?",
- new String[] { id }, null);
- while (dataCursor.moveToNext()) {
- String mimetype = dataCursor.getString(0); // 获取mimetype_id
- String data = dataCursor.getString(1); // 获取data1 列的内容
-
- // 3 根据 mimetype 类型 区分数据
- if ("vnd.android.cursor.item/email_v2".equals(mimetype)) {
- // 说明是data 是email 数据
- System.out.println("email~" + data);
- info.setEmail(data);
-
- } else if ("vnd.android.cursor.item/name".equals(mimetype)) {
- System.out.println("name~" + data);
- info.setName(data);
-
- } else if ("vnd.android.cursor.item/phone_v2"
- .equals(mimetype)) {
- System.out.println("phone~" + data);
- info.setPhone(data);
- }
-
- }
- dataCursor.close();
- contactInfos.add(info);
-
- }
-
- }
- cursor.close();
-
- return contactInfos;
-
- }
-
- }
getContentResolver().registerContentObserver(Uri.parse("content://sms"), true, new SmsObserver(new Handler()));
监听类:
- private final class SmsObserver extends ContentObserver{
- public SmsObserver(Handler handler) {
- super(handler);
- }
-
- public void onChange(boolean selfChange) {//查询发送箱中的短信(处于正在发送状态的短信放在发送箱)
- Cursor cursor = getContentResolver().query(Uri.parse("content://sms/outbox"),null, null, null, null);
- while(cursor.moveToNext()){
- StringBuilder sb = new StringBuilder();
- sb.append("_id=").append(cursor.getInt(cursor.getColumnIndex("_id")));
- sb.append(",address=").append(cursor.getString(cursor.getColumnIndex("address")));
- sb.append(";body=").append(cursor.getString(cursor.getColumnIndex("body")));
- sb.append(";time=").append(cursor.getLong(cursor.getColumnIndex("date")));
- Log.i("ReceiveSendSMS", sb.toString());
- } }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。