当前位置: 移动技术网 > IT编程>移动开发>Android > Android获取手机通讯录、sim卡联系人及调用拨号界面方法

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

更年期综合症症状,莱阳市人力资源和社会保障局,纵横校园 泻佳泉

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"/> 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网