zoukankan      html  css  js  c++  java
  • 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;
        }
    
        @Override
        public int delete(Uri uri, String s, String[] strings) {
            return 0;
        }
    
        @Override
        public int update(Uri uri, ContentValues contentValues, String s, String[] strings) {
            return 0;
        }
    )

    (2)在清单文件里注册内容提供者 (注意:第二个引号中的内容“包名.自定义名(有一定含义的名字)”,第二个引号中的内容“包名.类名”)

     <provider
                android: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;
        }
    
        @Override
        public int delete(Uri uri, String s, String[] strings) {
            return 0;
        }
    
        @Override
        public int update(Uri uri, ContentValues contentValues, String s, String[] strings) {
            return 0;
        }
    }
  • 相关阅读:
    【学习笔记】QT下载地址
    【学习笔记】激活码方式注册的实现原理述
    【学习笔记】Inputs on an Embedded Linux Device
    【学习笔记】TSlib校准原理
    【学习笔记】13.5 使用者的特殊 shell 与 PAM 模块
    【学习笔记】Linux GDM
    【学习笔记】MC.exe 生成H和RC文件
    【工具】时间差计算器
    【学习笔记】Android 学习(一)
    【学习笔记】tensor flow 2.4 moist 数据下载错误
  • 原文地址:https://www.cnblogs.com/sunrise-hs/p/5665338.html
Copyright © 2011-2022 走看看