Android 的 ContentProvider

开发自己应用的ContentProviderContentProvider是个抽象类,继承时,必须实现抽象方法)

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,对外提供数据库的创建及增删改查的方法。其中的重要参数URIURI 的分析起了很大的作用,Android提供了工具类:UriMatcher

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值