美文网首页述术
ContentResolver接口操作

ContentResolver接口操作

作者: 已经是咸鱼的小涛orz | 来源:发表于2017-07-15 18:05 被阅读143次

    内容解析者,昂,其实就是对ContentProvide提供的数据进行增删改查操作

    源码中ContentResolver的方法挺多了,不过咱只需要增删改查,其他也没啥操作,所以就只要关注insert、delete、update、query这四个接口了。

    public final Uri insert(Uri url, ContentValues values)
    public final int delete(Uri url, String where,  String[] selectionArgs)
    public final int update(Uri uri, ContentValues values, String where, String[] selectionArgs) 
    public final Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
    

    获取ContentResolver实例

    context.getContentResolver(); 
    

    insert

    public final Uri insert(Uri url, ContentValues values)
    

    ContentValues 和 HashTable 类似都是一种存储的机制,但是两者最大的区别就在于:ContentValues只能存储基本类型的数据,像string、int之类的,不能存储对象这种东西,而HashTable却可以存储对象。

    上一段新增日历信息的代码看下具体的使用。。。。。

    private Uri addCalendarEvent(Schedule schedule){
         ContentValues contentValues = new ContentValues();
         contentValues.put(CalendarContract.Events.CALENDAR_ID, account);
         contentValues.put(CalendarContract.Events.TITLE, schedule.getTitle());
         contentValues.put(CalendarContract.Events.EVENT_LOCATION, schedule.getLocation());
         contentValues.put(CalendarContract.Events.DESCRIPTION, schedule.getDescription());
         contentValues.put(CalendarContract.Events.DTSTART, schedule.getStartTime());
         contentValues.put(CalendarContract.Events.DTEND, schedule.getEndTime());
         contentValues.put(CalendarContract.Events.HAS_ALARM, 1);
         contentValues.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
         return mContext.getContentResolver().insert(Uri.parse(CALANDER_EVENT_URL), contentValues);
     }
    

    下面是 封装 的日历写入方法 * 需要联系之前ContentProvide管理系统日历的代码参考*。
    尝试将日程信息写入系统日历,失败的话则跳转到系统日历界面手动填写
    其实主要原因是上文提到的自建用户莫名消失的原因,导致该用户下的日历全部消失,所以更改了代码逻辑不再自行创建用户,因此没有用户的时候必定会写入错误,然后又比较懒,不想大规模改代码,所以就直接加个try catch 了.......囧rz

    public boolean insert(Schedule schedule){
       try {
          //  这两函数对照前面的内容看吧-。-
          setCalendarAlarm(setCalendarEvent(schedule));
          return true;
       }catch (Exception e){
          e.printStackTrace();
          Intent intent = new Intent(Intent.ACTION_INSERT)
                .setData(CalendarContract.Events.CONTENT_URI)
                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                .putExtra(CalendarContract.Events.TITLE, schedule.getTitle())
                .putExtra(CalendarContract.Events.DESCRIPTION, schedule.getDescription())
                .putExtra(CalendarContract.Events.EVENT_LOCATION, schedule.getLocation())
                .putExtra(CalendarContract.Events.DTSTART, schedule.getStartTime())
                .putExtra(CalendarContract.Events.DTEND, schedule.getEndTime())
                .putExtra(CalendarContract.Reminders.MINUTES, 0);
          mContext.startActivity(intent);
          return false;
       }
    }
    
    

     下班了先不写了,周一回来在写 



    昂,继续写-。-    周一写?不存在的 

    删 delete

    public final int delete(Uri url, String where,  String[] selectionArgs)
    

    where筛选条件,如果不进行筛选就传入null,比如只删除标题为“测试”的日程,
    应传入String where = CalendarContract.Events.TITLE + "= 测试";
    selectionArgs此参数需要配合上一个参数where一起使用,如果where中含有"?",那么selectionArgs的字段就会替换掉where中的"?"
    比如下面代码中的删除条件其实是  为了便于观察,语法格式上有问题
    CalendarContract.Events.TITLE = schedule.getTitle() AND CalendarContract.Events.DTSTART = timestamp

    public int delete(Schedule schedule){
        String timestamp =  String.valueOf(schedule.getStartTime());
        return mContext.getContentResolver().delete(
                Uri.parse(CALANDER_EVENT_URL),
                CalendarContract.Events.TITLE + " =? AND " + CalendarContract.Events.DTSTART + " =? ",
                new String[]{schedule.getTitle(), timestamp}
        );
    }
    

    查 query

    public final Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
    

    我屮艸芔茻,写一半发现参数都一样。。。。都在查询里写了得了。。。。
    uri这个就接口地址了,没啥好说的
    projection这个是要返回的内容Column,如果返回全部内容就传null,此时相当于sql的select * from TABLE
    selection这个就是上面delete说的where了,筛选条件,也就是sql 的 where
    selectionArgs结合selection一起使用,替换掉字符串中的 占位符 "?"
    sortOrder排序规则,默认是升序
    以下相当于sql语句select * from CALANDER_EVENT_URL where TITLE = title ORDER BY DTSTART DESC

    public void check(String title){
        Cursor eventCursor = null;
        try {
            eventCursor = mContext.getContentResolver().query(
                    Uri.parse(CALANDER_EVENT_URL),
                    null,
                    CalendarContract.Events.TITLE + " =? ",
                    new String[]{title},
                    ContactsContract.Events.DTSTART+ " DESC"
            );
            if (eventCursor != null && eventCursor.getCount() > 0){
                while (eventCursor.moveToNext()){
                    LogUtil.v("Calendar Title", eventCursor.getString(eventCursor.getColumnIndex(CalendarContract.Events.TITLE)));
                    LogUtil.v("Calendar Description", eventCursor.getString(eventCursor.getColumnIndex(CalendarContract.Events.DESCRIPTION)));
                    LogUtil.v("Calendar Date", eventCursor.getString(eventCursor.getColumnIndex(CalendarContract.Events.DTSTART)));
                }
            }else {
                LogUtil.v("Calendar Local", "none");
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (eventCursor != null) {
                eventCursor.close();
            }
        }
    }
    

    改 update

    public final int update(Uri uri, ContentValues values, String where, String[] selectionArgs) 
    

    示例

    public int update(Schedule oldSc, Schedule schedule){
        ContentValues contentValues = new ContentValues();
        contentValues.put(CalendarContract.Events.TITLE, schedule.getTitle());
        contentValues.put(CalendarContract.Events.EVENT_LOCATION, schedule.getLocation());
        contentValues.put(CalendarContract.Events.DESCRIPTION, schedule.getDescription());
        contentValues.put(CalendarContract.Events.DTSTART, schedule.getStartTime());
        contentValues.put(CalendarContract.Events.DTEND, schedule.getEndTime());
        return mContext.getContentResolver().update(
            Uri.parse(CALANDER_EVENT_URL),
                contentValues,
                CalendarContract.Events.TITLE + " =? AND " + CalendarContract.Events.DTSTART + " =? ",
                new String[]{oldSc.getTitle(), String.valueOf(oldSc.getStartTime())}
        );
    }
    

    相关文章

      网友评论

        本文标题:ContentResolver接口操作

        本文链接:https://www.haomeiwen.com/subject/rmjwhxtx.html