return CONTENT_TYPE;
case SMS_ITEM:
return CONTENT_ITEM_TYPE;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
4. 实现query, insert, delete, update四个操作。
具体的实现可以用sqlite, file等。并根据uri分情况操作。
a. query时如果是item加查询条件id.
where = "_ID=" + uri.getPathSegments().get(1) + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : "";
最后要加上
cursor.setNotificationUri(getContext().getContentResolver(), uri);
b. insert时要求uri只能是dir. 成功之后返回一个加id的uri.
Uri insertUri = ContentUris.withAppendedId(CONTENT_URI, rowId);
c. update、delete与query差不多。
//注意通知注册uri的观察者。
getContext().getContentResolver().notifyChange(uri, null);
5. 在AndroidManifest.xml中定义
provider元素,主要属性有:
复制代码
name => ContentProvider类名
authorities => content type的授权部分
multiprocess => true允许在每个客户进程中创建provider实例,消除执行IPC的需求。
注:相关教程知识阅读请移步到Android开发频道。










