ContentResolve内容访问者(访问系统)

访问联系人首先你要找到uri的路径:

<1>content://com.android.contacts/raw_contacts(获得联系人的id,姓名)
    <2>content://com.android.contacts/raw_contacts/"+id+"/data(根据联系人的id获得联系人的信息)

在写之前要写一个权限(读取联系人):

    <!--读取联系人权限-->
    <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>

1.界面就写了一个Button
2.在MainActivity.java中中写了一个点击的方法:

  public void getContacts(View view){
        //获取联系人的ID及名字
        Uri uri=Uri.parse("content://com.android.contacts/raw_contacts");
        Cursor cursor=cr.query(uri,null,null,null,null);
        while(cursor.moveToNext()){
            //id
            int id=cursor.getInt(cursor.getColumnIndex("_id"));
            //nane
            String name=cursor.getString(cursor.getColumnIndex("display_name"));
            Log.i("test",id+" "+name);

            //根据联系人的ID获取联系人相对应的数据(每个id的全部数据)
            Uri uriData=Uri.parse("content://com.android.contacts/raw_contacts/"+id+"/data");
            Cursor cursorData=cr.query(uriData,null,null,null,null);


            while(cursorData.moveToNext()){
               String data1=cursorData.getString(cursorData.getColumnIndex("data1"));
                // 会出这个错(Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 79 columns.)肯定是类名写错了
                int type=cursorData.getInt(cursorData.getColumnIndex("mimetype_id"));
                //访问联系人不能用这个表的类名,要调用另一个表中的中的列
//                String type=cursorData.getString(cursorData.getColumnIndex("mimetype"));
                //判断vnd.android.cursor.item/phone_v2是这个就显示
                if("vnd.android.cursor.item/phone_v2".equals(type)){
                    Log.i("test","   "+data1+"    "+type);
                }
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值