开发自己应用的ContentProvider(ContentProvider是个抽象类,继承时,必须实现抽象方法)
1构造CONTENT_URI
CONTENT_URI="content://" + AUTHORITY + "/diaries"
"content://" 这部分一直存在,不用修改
AUTHORITY(授权)要求是唯一,而且和Manifest当中provider标签的AUTHORITY内容一致
例如:
public static final String AUTHORITY = "com.ex09_2_contentprovider.diarycontentprovider";
<provider android:name="DiaryContentProvider"
android:authorities="com.ex09_2_contentprovider.diarycontentprovider" />
"/diaries" 请求数据的类型
2 构建用户的数据存储系统。可以使文件存储也可以是数据库存储(使用数据库存储的居多)继承ContentProvider 重写以下方法
abstract boolean | onCreate() Implement this to initialize your content provider on startup. |
abstract String | getType(Uri uri) Implement this to handle requests for the MIME type of the data at the given URI. |
abstract Cursor | query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) Implement this to handle query requests from clients. |
abstract Uri | insert(Uri uri, ContentValues values) Implement this to handle requests to insert a new row. |
abstract int | update(Uri uri, ContentValues values, String selection, String[] selectionArgs) Implement this to handle requests to update one or more rows. |
abstract int | delete(Uri uri, String selection, String[] selectionArgs) Implement this to handle requests to delete one or more rows. |
使用数据库存储,还是需要SQLiteOpenHelper 和SQLiteDatabase
通过ContentProvider,对外提供数据库的创建及增删改查的方法。其中的重要参数URI。URI 的分析起了很大的作用,Android提供了工具类:UriMatcher