zoukankan      html  css  js  c++  java
  • [android] 内容提供者实现

    [android] 内容提供者实现

    上一节的主机名类似网络上的域名,协议是content://,可以定义一下规则

    content://主机名/insert 添加操作

    content://主机名/delete 删除操作

    content://主机名/query 查询操作

    content://主机名/update 修改操作

    规则定义好之后,我们需要解析一下这个字符串,系统提供了一个api来匹配这个字符串

    UriMatcher类,new出来对象new UriMatcher(code) code是个int,表示未匹配到的返回值,一般使用常量UriMatcher.NO_MATCH-1,把他定义成静态成员属性。

    Uri也可以加个#代表的是数字,使用ContentUris类的parseId()方法取出#的值

    定义一个静态代码块来测试一下这个matcherstatic{},调用UriMatcher对象的addURI(authorities,path,code)方法,参数authoritiesString主机名,pathString操作名,codeint匹配成功的返回值一般定义成常量private static final int增删查改四个常量。这个就是添加一组匹配规则

    实现的主要方法

    1.onCreate()方法

    当内容提供者这个类开启的时候回调此方法,初始化数据库帮助对象如PersonSQLiteHelper

    2.query(uri,projection,selection,selectArgs,sortOrder) (参数:Uri对象别人传过来的uri,字段,条件,条件对应的参数,排序)

    方法里面调用matcher对象的match(uri)方法,对所传的uri进行匹配,如果成功就返回上面定义的匹配码,匹配成功调用helper对象的getReadableDatabase()方法获取数据库对象,调用db对象的query(table,columns,selection,selectionArgs,groupby,having,orderBy)方法得到Cursor结果集对象,细节db不要close了,框架会自动关闭

    3.getType(uri),返回这个urimime类型

    返回一条数据return “vnd.android.cursor.item”

    返回一组数据return “vnd.android.cursor.dir”

    测试这个ContentProvider,新建一个应用,得到手机的中间人,通过getContentResolver()方法获取ContentResolver对象

    调用ContentResolver对象的query(uri,projection,selection,selectArgs,sortOrder)方法,(参数:Uri对象别人传过来的uri,字段,条件,条件对应的参数,排序),返回Cursor对象。

    获取Uri对象,通过Uri类的parse(uriString)方法,参数:content://主机名/操作名

    While循环Cursor对象。

    测试报错权限问题 清单文件加这个 android:exported="true"

    ddms面板上左侧的进程列表,点击上面工具栏的stop按钮,关闭进程,当我调用ContentProvider的时候,进程会再次开启。

    大部分时候,我们都没有机会去写这个ContentProvider,明白这个原理之后,就要进入重点了,当我们要取出联系人的数据,短信的数据,浏览器书签的数据等的时候,我们就需要使用到它了,通过阅读源代码搞明白他们的Uri和需要传入的参数,更好的使用它

    内容提供者:

    package com.tsh.database;
    
    import android.content.ContentProvider;
    import android.content.ContentValues;
    import android.content.UriMatcher;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.net.Uri;
    
    public class PersonDbProvider extends ContentProvider {
        private static final int INSERT = 1;
        private static final int SELECT = 2;
        private static final int DELETE = 3;
        private static final int UPDATE = 4;
        private static UriMatcher matcher=new UriMatcher(UriMatcher.NO_MATCH);
        private PersonSQLiteOpenHelper helper;
        static{
            //增加一组规则
            matcher.addURI("com.tsh.database.personprovider", "insert", INSERT);
            matcher.addURI("com.tsh.database.personprovider", "select", SELECT);
            matcher.addURI("com.tsh.database.personprovider", "delete", DELETE);
            matcher.addURI("com.tsh.database.personprovider", "update", UPDATE);
        }
        //初始化helper
        @Override
        public boolean onCreate() {
            helper=new PersonSQLiteOpenHelper(getContext());
            return false;
        }
        //查询
        @Override
        public Cursor query(Uri uri, String[] projection, String selection,
                String[] selectionArgs, String sortOrder) {
            //验证
            if(matcher.match(uri)==SELECT){
                SQLiteDatabase db=helper.getReadableDatabase();
                Cursor cursor=db.query("person", projection, selection, selectionArgs, null, null, sortOrder);
                return cursor;
            }
            return null;
        }
    
        @Override
        public String getType(Uri uri) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public Uri insert(Uri uri, ContentValues values) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public int delete(Uri uri, String selection, String[] selectionArgs) {
            // TODO Auto-generated method stub
            return 0;
        }
    
        @Override
        public int update(Uri uri, ContentValues values, String selection,
                String[] selectionArgs) {
            // TODO Auto-generated method stub
            return 0;
        }
    
    }

    测试:

            ContentResolver resolver=getContentResolver();
            Uri uri=Uri.parse("content://com.tsh.database.personprovider/select");
            Cursor cursor=resolver.query(uri, null, null, null, null);
            while(cursor.moveToNext()){
                String name=cursor.getString(cursor.getColumnIndex("name"));
                System.out.println("name"+name);
            }
            cursor.close();
  • 相关阅读:
    python测试开发django177.启动项目添加初始化数据(fixtures的使用) 上海
    python笔记70 Python中`__repr__`和`__str__`区别 上海
    python笔记69 什么是猴子补丁(Monkey Patch)? 上海
    pytest文档78 钩子函数pytest_runtest_makereport获取用例执行报错内容和print内容 上海
    python测试开发django176.数据库迁移数据(manage.py dumpdata) 上海
    python3面试题:如何用python实现栈(Stack)的操作? 上海
    python笔记71 traceback.print_exc()保存异常内容 上海
    测试驱动开发 Rss Reader Item Marker
    Rhino Mocks (RhinoMock)2
    继承的Singleton模式的实现
  • 原文地址:https://www.cnblogs.com/taoshihan/p/5270903.html
Copyright © 2011-2022 走看看