欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android获取手机通讯录、sim卡联系人及调用拨号界面方法

程序员文章站 2022-03-08 17:48:33
android获取手机通讯录联系人信息 复制代码 代码如下: private void getphonecontacts() {   ...

android获取手机通讯录联系人信息

复制代码 代码如下:

private void getphonecontacts() {   
    contentresolver resolver = this.getcontentresolver();   
       
    // 获取手机联系人   
   cursor phonecursor = resolver.query(phone.content_uri, 
                new string[] { phone.contact_id, phone.display_name, 
                        phone.number }, 
                phone.display_name + "=?" + " and " + phone.type + "='" 
                        + phone.type_mobile + "'", new string[] { name }, null); 
    if (phonecursor != null) { 
        while (phonecursor.movetonext()) { 
            string number = phonecursor.getstring(2); 
                    // 当手机号码为空的或者为空字段 跳过当前循环   
                    if (textutils.isempty(phonenumber))   
                        continue; 
            // 得到联系人名称               
            string username = phonecursor.getstring(1); 
            mcontactsname.add(contactname);   
                mcontactsnumber.add(phonenumber); 
 
        } 
        phonecursor.close(); 
    }  

获得手机sim卡联系人信息

sim卡和手机本人 获取的方式类似 只是url有点不一样 ,须要注意的一点是 sim卡  是没有联系人头像的。

复制代码 代码如下:

private void getsimcontacts() {   
    contentresolver resolver = mcontext.getcontentresolver();   
    // 获取sims卡联系人   
    uri uri = uri.parse("content://icc/adn");   
    cursor phonecursor = resolver.query(uri, 
                new string[] { phone.contact_id, phone.display_name, 
                        phone.number }, 
                phone.display_name + "=?" + " and " + phone.type + "='" 
                        + phone.type_mobile + "'", new string[] { name }, null); 
    if (phonecursor != null) { 
        while (phonecursor.movetonext()) { 
            string number = phonecursor.getstring(2); 
                    // 当手机号码为空的或者为空字段 跳过当前循环   
                    if (textutils.isempty(phonenumber))   
                        continue; 
            // 得到联系人名称               
            string username = phonecursor.getstring(1); 
            mcontactsname.add(contactname);   
                mcontactsnumber.add(phonenumber); 
 
        } 
        phonecursor.close(); 
    } 


调用系统拨打电话的界面 ,代码如下。
tel:电话号码
复制代码 代码如下:

//调用系统方法拨打电话   
    intent dialintent = new intent(intent.action_call, uri.parse("tel:" + mcontactsnumber.get(position)));   
    startactivity(dialintent);  

最后,千万别忘记在androidmanifest.xml文件中添加权限,否则运行程序是报错!

复制代码 代码如下:

<!-- 读取联系*限 -->    
<uses-permission android:name="android.permission.read_contacts"/>   
<!-- 拨打电话权限 -->   
<uses-permission android:name="android.permission.call_phone"/>