内容提供者(掌握)

  • 应用的数据库是不允许其他应用访问的
  • 内容提供者的作用就是让别的应用访问到你的私有数据
  • 自定义内容提供者,继承ContentProvider类,重写增删改查方法,在方法中写增删改查数据库的代码,举例增方法

    @Override
    public Uri insert(Uri uri, ContentValues values) {
    db.insert("person", null, values);
    return uri;
    }
  • 在清单文件中定义内容提供者的标签,注意必须要有authorities属性,这是内容提供者的主机名,功能类似地址

    <provider android:name="com.itheima.contentprovider.PersonProvider"
    android:authorities="com.itheima.person"
    android:exported="true"
    ></provider>
  • 创建一个其他应用,访问自定义的内容提供者,实现对数据库的插入操作

    public void click(View v){
    //得到内容分解器对象
    ContentResolver cr = getContentResolver();
    ContentValues cv = new ContentValues();
    cv.put("name", "小方");
    cv.put("phone", 138856);
    cv.put("money", 3000);
    //url:内容提供者的主机名
    cr.insert(Uri.parse("content://com.itheima.person"), cv);
    }

UriMatcher(掌握)

  • 用于判断一条uri跟指定的多条uri中的哪条匹配
  • 添加匹配规则

    //指定多条uri
    um.addURI("com.itheima.person", "person", PERSON_CODE);
    um.addURI("com.itheima.person", "company", COMPANY_CODE);
    //#号可以代表任意数字
    um.addURI("com.itheima.person", "person/#", QUERY_ONE_PERSON_CODE);
  • 通过Uri匹配器可以实现操作不同的表

    @Override
    public Uri insert(Uri uri, ContentValues values) {
    if(um.match(uri) == PERSON_CODE){
    db.insert("person", null, values);
    }
    else if(um.match(uri) == COMPANY_CODE){
    db.insert("company", null, values);
    }
    else{
    throw new IllegalArgumentException();
    }
    return uri;
    }
  • 如果路径中带有数字,把数字提取出来的api

    int id = (int) ContentUris.parseId(uri);

短信数据库(掌握)

  • 只需要关注sms表
  • 只需要关注4个字段
    • body:短信内容
    • address:短信的发件人或收件人号码(跟你聊天那哥们的号码)
    • date:短信时间
    • type:1为收到,2为发送

读取系统短信,首先查询源码获得短信数据库内容提供者的主机名和路径,然后访问内容提供者(掌握)

    ContentResolver cr = getContentResolver();
Cursor c = cr.query(Uri.parse("content://sms"), new String[]{"body", "date", "address", "type"}, null, null, null);
while(c.moveToNext()){
String body = c.getString(0);
String date = c.getString(1);
String address = c.getString(2);
String type = c.getString(3);
System.out.println(body+";" + date + ";" + address + ";" + type);
}

插入系统短信(熟悉)

    ContentResolver cr = getContentResolver();
ContentValues cv = new ContentValues();
cv.put("body", "您尾号为XXXX的招行储蓄卡收到转账1,000,000人民币");
cv.put("address", 95555);
cv.put("type", 1);
cv.put("date", System.currentTimeMillis());
cr.insert(Uri.parse("content://sms"), cv);
  • 插入查询系统短信需要注册权限

联系人数据库(掌握)

  • raw_contacts表:

    • contact_id:联系人id
  • data表:联系人的具体信息,一个信息占一行
    • data1:信息的具体内容
    • raw_contact_id:联系人id,描述信息属于哪个联系人
    • mimetype_id:描述信息是属于什么类型
  • mimetypes表:通过mimetype_id到该表查看具体类型

读取联系人(掌握)

  • 先查询raw_contacts表拿到联系人id

    Cursor cursor = cr.query(Uri.parse("content://com.android.contacts/raw_contacts"), new String[]{"contact_id"}, null, null, null);
  • 然后拿着联系人id去data表查询属于该联系人的信息

    Cursor c = cr.query(Uri.parse("content://com.android.contacts/data"), new String[]{"data1", "mimetype"}, "raw_contact_id = ?", new String[]{contactId}, null);
  • 得到data1字段的值,就是联系人的信息,通过mimetype判断是什么类型的信息

    while(c.moveToNext()){
    String data1 = c.getString(0);
    String mimetype = c.getString(1);
    if("vnd.android.cursor.item/email_v2".equals(mimetype)){
    contact.setEmail(data1);
    }
    else if("vnd.android.cursor.item/name".equals(mimetype)){
    contact.setName(data1);
    }
    else if("vnd.android.cursor.item/phone_v2".equals(mimetype)){
    contact.setPhone(data1);
    }
    }

插入联系人(熟悉)

  • 先查询raw_contacts表,确定新的联系人的id应该是多少
  • 把确定的联系人id插入raw_contacts表

    cv.put("contact_id", _id);
    cr.insert(Uri.parse("content://com.android.contacts/raw_contacts"), cv);
  • 在data表插入数据

    • 插3个字段:data1、mimetype、raw_contact_id

      cv = new ContentValues();
      cv.put("data1", "赵六");
      cv.put("mimetype", "vnd.android.cursor.item/name");
      cv.put("raw_contact_id", _id);
      cr.insert(Uri.parse("content://com.android.contacts/data"), cv); cv = new ContentValues();
      cv.put("data1", "1596874");
      cv.put("mimetype", "vnd.android.cursor.item/phone_v2");
      cv.put("raw_contact_id", _id);
      cr.insert(Uri.parse("content://com.android.contacts/data"), cv);

内容观察者(掌握)

  • 当数据库数据改变时,内容提供者会发出通知,在内容提供者的uri上注册一个内容观察者,就可以收到数据改变的通知

    cr.registerContentObserver(Uri.parse("content://sms"), true, new MyObserver(new Handler()));
    
    class MyObserver extends ContentObserver{
    
        public MyObserver(Handler handler) {
    super(handler);
    // TODO Auto-generated constructor stub
    } //内容观察者收到数据库发生改变的通知时,会调用此方法
    @Override
    public void onChange(boolean selfChange) { } }
  • 在内容提供者中发通知的代码

    ContentResolver cr = getContext().getContentResolver();
    //发出通知,所有注册在这个uri上的内容观察者都可以收到通知
    cr.notifyChange(uri, null);

Android基础总结(十)的更多相关文章

  1. <Android 基础(十九)> CoordinatorLayout

    介绍 CoordinatorLayout,中文翻译,协调布局,顾名思义,此布局中的子View之间,子View与父布局之间应该是可以协调工作的,如何协调,Behavior. 今天看下Android St ...

  2. <Android 基础(十八)> XLIFF

    介绍 XLIFF ,XML Localization Interchange File Format,XML本地化数据交换格式. 实际使用 1.布局文件 activity_main.xml <? ...

  3. <Android 基础(十六)> Toast

    介绍 A toast provides simple feedback about an operation in a small popup. It only fills the amount of ...

  4. <Android 基础(十五)> Alert Dialog

    介绍 The AlertDialog class allows you to build a variety of dialog designs and is often the only dialo ...

  5. <Android 基础(十四)> selector

    介绍 A StateListDrawable is a drawable object defined in XML that uses a several different images to r ...

  6. <Android 基础(十二)> TextInputLayout,让输入框更有灵性

    介绍 Layout which wraps an {@link android.widget.EditText} (or descendant) to show a floating label wh ...

  7. <Android 基础(十)> FloatingActionButton

    介绍 Source Code中的介绍如下: Floating action buttons are used for a special type of promoted action. They a ...

  8. 有史来最大改变 Android 5.0十大新特性

    有史来最大改变 Android 5.0十大新特性 2014.10.16 14:51:31 来源:腾讯数码作者:腾讯数码 ( 0 条评论 )   距离Android系统上一次重大更新不到一年的时间,谷歌 ...

  9. Android基础夯实--重温动画(二)之Frame Animation

    心灵鸡汤:天下事有难易乎,为之,则难者亦易矣:不为,则易者亦难矣. 摘要 当你已经掌握了Tween Animation之后,再来看Frame Animation,你就会顿悟,喔,原来Frame Ani ...

  10. Android基础夯实--重温动画(五)之属性动画 ObjectAnimator详解

    只有一种真正的英雄主义 一.摘要 ObjectAnimator是ValueAnimator的子类,它和ValueAnimator一样,同样具有计算属性值的功能,但对比ValueAnimator,它会更 ...

随机推荐

  1. 类型转换和类型相关函数.png

  2. BI解决方案分享:地产BI数据分析系统的建设

    近几年中国地产行业发展迅猛,行业整合已成大势所趋,逐步由区域开发转变为集团化的跨地区综合开发商.然而,对于处在超常规速度发展的房地产企业来说,其面临的挑战也是超常规的.企业要在有限的资金和人力条件下, ...

  3. 【转】Android SDK Samples,学习Android的好方法

    转载地址:http://blog.csdn.net/rowland001/article/details/50886288 从今天开始呢,我要开始学习Google家自己出的Android代码示例,总觉 ...

  4. UI-初识君面之理论篇

    一个好的app不光要用好的功能,还要有好的界面,这样内外兼修才算得上是一个好的App.其实跟人一样,不能只刷帅,要有内涵(看清楚哦,内涵不是指闷骚).不知不觉在园子里已经晃了八年,来深也八年了,.NE ...

  5. RAC textView的双向绑定

    今天在写关于textView的数据绑定时原先写法是这样的: p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #78 ...

  6. iOS网络1——NSURLConnection使用详解

    原文在此 一.整体介绍 NSURLConnection是苹果提供的原生网络访问类,但是苹果很快会将其废弃,且由NSURLSession(iOS7以后)来替代.目前使用最广泛的第三方网络框架AFNetw ...

  7. swift学习笔记2——函数、闭包

    之前学习swift时的个人笔记,根据github:the-swift-programming-language-in-chinese学习.总结,将重要的内容提取,加以理解后整理为学习笔记,方便以后查询 ...

  8. 获取View的截图-将View转换为Bitmap对象

    开发中,有时候需要获取View的截图来做动画来达到动画流程的目的 原理:将View的内容画到一个Bitmap画布上,然后取出 下面封装了一个从View生成Bitmap的工具类 /** * 将View转 ...

  9. tuple放入dict中

    tuple放入dict中是否可以正常运行 # 将tuple放入dict中 a = ('AI','Kobe','Yao') b = ('AI',['Kobe','Yao']) dict1 = {'a': ...

  10. 探索逻辑事务 TransactionScope

    一.什么是TransactionScope? TransactionScope即范围事务(类似数据库中的事务),保证事务声明范围内的一切数据修改操作状态一致性,要么全部成功,要么全部失败回滚. MSD ...