Android利用ContentProvider获取联系人信息

 更新时间:2021年11月15日 08:24:00   作者:安之若素i  
这篇文章主要为大家详细介绍了Android利用ContentProvider获取联系人信息,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Android利用ContentProvider获取联系人信息的具体代码,供大家参考,具体内容如下

在写代码前我们首先看一下运行的效果

运行效果如下:

点了获取联系人就展示如下效果

读取联系人信息的例子(MainActivity)

package com.example.administrator.myapplication;

import android.content.ContentResolver;
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.ListView;
import android.widget.SimpleAdapter;

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

/**
 * 获取联系人
 */
public class MainActivity extends AppCompatActivity {

    private ContentResolver cr;
    private List<Map<String,Object>> data;
    private ListView lv_main_list;
    private SimpleAdapter simpleAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv_main_list = (ListView)findViewById(R.id.lv_main_list);

        //获取内容访问者
        cr = getContentResolver();

        data = new ArrayList<Map<String,Object>>();
        //适配器
        simpleAdapter = new SimpleAdapter(this,data,android.R.layout.simple_list_item_2,new String[]{"names","phones"},new int[]{android.R.id.text1,android.R.id.text2});
        lv_main_list.setAdapter(simpleAdapter);
    }

    public void getContacts(View view){
        Cursor cursor=cr.query(Uri.parse("content://com.android.contacts/raw_contacts"),null,null,null,null);
        while(cursor.moveToNext()){
            Map<String,Object> map=new HashMap<String,Object>();
            int id=cursor.getInt(cursor.getColumnIndex("_id"));
            String displayName=cursor.getString(cursor.getColumnIndex("display_name"));
            Log.i("test",id+" "+displayName);
            map.put("names",displayName);

            //根据联系人获取联系人数据
            Cursor cursor2=cr.query(Uri.parse("content://com.android.contacts/raw_contacts/"+id+"/data"),null,null,null,null);
            while(cursor2.moveToNext()){
              //  int type=cursor2.getInt(cursor2.getColumnIndex("mimetype_id"));
                String type=cursor2.getString(cursor2.getColumnIndex("mimetype"));
                String data1=null;
                if ("vnd.android.cursor.item/phone_v2".equals(type)){
                    data1 = cursor2.getString(cursor2.getColumnIndex("data1"));
                    Log.i("test","   "+type+" "+data1);
                    map.put("phones",data1);
                }
            }
            data.add(map);
        }
        //通知适配器发生改变
        simpleAdapter.notifyDataSetChanged();
    }
}

布局文件(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.administrator.myapplication.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="获取所有的联系人"
        android:onClick="getContacts"
        />
    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lv_main_list"

        ></ListView>
</LinearLayout>

需要在清单文件上配置联系人的权限(AndroidManifest.xml)

<uses-permission android:name="android.permission.READ_CONTACTS" />

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android实现拖动小球跟随手指移动效果

    Android实现拖动小球跟随手指移动效果

    这篇文章主要为大家详细介绍了Android实现拖动小球跟随手指移动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-03-03
  • Android自定义控件的创建方法

    Android自定义控件的创建方法

    这篇文章主要为大家详细介绍了Android自定义控件的创建方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-04-04
  • 微信小程序 canvas开发实例及注意事项

    微信小程序 canvas开发实例及注意事项

    这篇文章主要介绍了微信小程序 wxcanvas开发实例及注意事项的相关资料,这里对微信canvas与H5中的canvas做对比,并说明注意事项,需要的朋友可以参考下
    2016-12-12
  • 基于Android RxCache使用方法详解

    基于Android RxCache使用方法详解

    下面小编就为大家分享一篇基于Android RxCache使用方法详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-01-01
  • Android游戏开发学习之引擎用法实例详解

    Android游戏开发学习之引擎用法实例详解

    这篇文章主要介绍了Android游戏开发学习之引擎用法,较为详细的分析了Android游戏开发中所常用的JBox2D引擎功能及相关使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-10-10
  • Android 动画之AlphaAnimation应用详解

    Android 动画之AlphaAnimation应用详解

    本节讲解AlphaAnimation 动画,窗口的动画效果,淡入淡出什么的,有些游戏的欢迎动画,logo的淡入淡出效果就使用AlphaAnimation,具体的祥看本文,需要的朋友可以参考下
    2012-12-12
  • Android实现屏幕旋转方法总结

    Android实现屏幕旋转方法总结

    这篇文章主要介绍了Android实现屏幕旋转方法,实例总结了屏幕旋转的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-04-04
  • android开发实现文件读写

    android开发实现文件读写

    这篇文章主要为大家详细介绍了android开发实现文件读写,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-07-07
  • 限时抢购秒杀系统架构分析与实战

    限时抢购秒杀系统架构分析与实战

    这篇文章主要介绍了限时抢购秒杀系统架构分析与实战 的相关资料,需要的朋友可以参考下
    2016-01-01
  • Android自定义控件深入学习 Android生成随机验证码

    Android自定义控件深入学习 Android生成随机验证码

    这篇文章主要再次为大家介绍了Android自定义控件,以及针对自定义view学习,实战演练了Android生成随机验证码的详细过程,感兴趣的小伙伴们可以参考一下
    2016-01-01

最新评论