Android:ContentProvider获取手机联系人列表

这次带来的是手机通讯录常用字段的展示

直接上代码,看注释:

activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.android25_resolver.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="获取所有联系人"
        android:onClick="getContacts"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="获取所有短信"
        android:onClick="getsms"
        />
    <ListView
        android:layout_width="match_parent"
        android:id="@+id/ls_contacts"
        android:layout_height="wrap_content">
    </ListView>
</LinearLayout>

item_listview.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="50dp">
    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:id="@+id/tv_item_list_id"
        />

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:id="@+id/tv_item_list_name"
        />
    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:id="@+id/tv_item_list_number"
        />


</LinearLayout>

MainActivity.java:
package com.example.android25_resolver;

import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static android.R.attr.id;

public class MainActivity extends AppCompatActivity {

    private ContentResolver cr;
    private ListView ls_contacts;
    private List<Map<String,String>> mp=new ArrayList<>();
    private SimpleAdapter simpleAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //拿到内容访问者
        cr = getContentResolver();
        //得到listView
        ls_contacts = (ListView) findViewById(R.id.ls_contacts);
        //实例一个简易适配器
        simpleAdapter = new SimpleAdapter(this,mp, R.layout.item_listview,new String[]{"name","phone"},new int[]{R.id.tv_item_list_name,R.id.tv_item_list_number} );
        //给listView设置适配器
        ls_contacts.setAdapter(simpleAdapter);
    }


    public  void getsms(View view){
        Intent intent=new Intent(this,SMSActivity.class);
        startActivity(intent);
    }


    public void getContacts(View view){
        Uri uri=Uri.parse("content://com.android.contacts/raw_contacts");
        Cursor cs=cr.query(uri,null,null,null,null,null);
        while(cs.moveToNext()){
            //拿到联系人id 跟name
            int id=cs.getInt(cs.getColumnIndex("_id"));
            String name=cs.getString(cs.getColumnIndex("display_name"));
            //得到这个id的所有数据(data表)
            Uri uri1=Uri.parse("content://com.android.contacts/raw_contacts/"+id+"/data");
            Cursor cs2=cr.query(uri1,null,null,null,null,null);
            Map<String,String>  maps=new HashMap<>();//实例化一个map
            while ( cs2.moveToNext()){
                //得到data这一列 ,包括很多字段
                String data1=cs2.getString(cs2.getColumnIndex("data1"));
                //得到data中的类型
                String type=cs2.getString(cs2.getColumnIndex("mimetype"));
                String str=type.substring(type.indexOf("/")+1,type.length());//截取得到最后的类型
                if("name".equals(str)){//匹配是否为联系人名字
                    maps.put("name",data1);
                }if("phone_v2".equals(str)){//匹配是否为电话
                    maps.put("phone",data1);
                }
                Log.i("test",data1+"       "+type);
            }
            mp.add(maps);//将map加入list集合中
        }
        simpleAdapter.notifyDataSetChanged();//通知适配器发生数据改变

        Button button=(Button)view;//得到button
        button.setEnabled(false);//设置不可点击
    }
}

最后别忘了添加权限:
<!-- 添加读取联系人权限 -->
    <uses-permission android:name="android.permission.READ_CONTACTS" />

                 希望给初学者带来帮助。。。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值