ContentProvider的创建和使用
生活随笔
收集整理的這篇文章主要介紹了
ContentProvider的创建和使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
(1)首先創建內容提供者,實現暴露數據庫程序的功能 定義一個類繼承android.content包下的ContentProvider類,ContentProvider是一個抽象類,使用該類時重寫????? onCreate() getType() query() insert() delete() update()抽 象方法。
????????
public class PersonDBProvider extends ContentProvider {public boolean onCreate() {return false;}public String getType(Uri uri) {return null;}public Cursor query(Uri uri, String[] strings, String s, String[] strings1, String s1) {return null;}public Uri insert(Uri uri, ContentValues contentValues) {return null;}@Overridepublic int delete(Uri uri, String s, String[] strings) {return 0;}@Overridepublic int update(Uri uri, ContentValues contentValues, String s, String[] strings) {return 0;} )?
(2)在清單文件里注冊內容提供者 (注意:第二個引號中的內容“包名.自定義名(有一定含義的名字)”,第二個引號中的內容“包名.類名”)
<providerandroid:authorities="com.example.hanshu.first.contentResolver"android:name="com.example.hanshu.first.PersonDBProvider"></provider>?
(3)定義匹配器和添加匹配規則
?????? 1. 在PersonDBProvider類中定義一個uri的配置器,用于匹配uri,如果路徑不滿足條件,返回-1;
?????? 2. 添加匹配規則
?????????? 具體代碼如下:
??????????
public class PersonDBProvider extends ContentProvider {private static UriMatcher matcher=new UriMatcher(UriMatcher.NO_MATCH);private static final int INSERT=1;private static final int QUERY=2;private static final int DELETE=3;private static final int UPDATE=4;static {matcher.addURI("com.example.hanshu.first.contentResolver","insert",INSERT);matcher.addURI("com.example.hanshu.first.contentResolver","query",QUERY);matcher.addURI("com.example.hanshu.first.contentResolver","delete",DELETE);matcher.addURI("com.example.hanshu.first.contentResolver","update",UPDATE);}public boolean onCreate() {return false;}public String getType(Uri uri) {return null;}public Cursor query(Uri uri, String[] strings, String s, String[] strings1, String s1) {return null;}public Uri insert(Uri uri, ContentValues contentValues) {return null;}@Overridepublic int delete(Uri uri, String s, String[] strings) {return 0;}@Overridepublic int update(Uri uri, ContentValues contentValues, String s, String[] strings) {return 0;} }?
轉載于:https://www.cnblogs.com/sunrise-hs/p/5665338.html
總結
以上是生活随笔為你收集整理的ContentProvider的创建和使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 将列表的元素去重
- 下一篇: JS 根据子网掩码,网关计算出所有的IP