首页 > 其他 > 详细

ContentProvider的创建和使用

时间:2016-07-13 01:21:56      阅读:222      评论:0      收藏:0      [点我收藏+]

(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;
    }
}

 

ContentProvider的创建和使用

原文:http://www.cnblogs.com/sunrise-hs/p/5665338.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!