当前位置:操作系统 > 安卓/Android >>

android-调用本机的联系人界面及返回 - 随心

首先必加的权限<uses-permission android:name="android.permission.READ_CONTACTS" />

这里写两个方法。

方法一:本人在小米手机测试可以,不过在其他的手机测试发现返回的Uri为空,不知为何,于是找到了第二种方法来解决。

跳转至电话本
[java] www.zzzyk.com
       
Intent i = new Intent(Intent.ACTION_PICK); 
i.setType("vnd.android.cursor.dir/phone"); 
startActivityForResult(i, SEND_SMS_TYPE); 
返回获取数据

[java] 
        
    @Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    switch (requestCode) { 
    case SEND_SMS_TYPE: 
        if(!(resultCode == RESULT_OK)){ 
            return ; 
        } 
        if (data == null) { 
            return; 
        } 
        Uri uri = data.getData(); 
        Cursor cursor = getContentResolver().query(uri, null, null, null, 
                null); 
        cursor.moveToFirst(); 
 
        String number = cursor.getString(cursor 
                .getColumnIndexOrThrow(Phones.NUMBER)); 
        sendAdrress = number; 
        SEND_TYPE = SEND_SMS_TYPE; 
        send_text_SMS.setText("发送短信" + " : " + number); 
 
        break; 
    } 
    super.onActivityResult(requestCode, resultCode, data); 

方法二:

[java] 
Intent i = new Intent(); 
i.setAction(Intent.ACTION_PICK); 
i.setData(ContactsContract.Contacts.CONTENT_URI); 
startActivityForResult(i, SEND_SMS_TYPE); 
//----------------------------------------------------------------- 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
        switch (resultCode) { 
        case RESULT_OK: 
            switch (requestCode) { 
            case SEND_SMS_TYPE: 
                if (data == null) { 
                    return; 
                } 
                String phoneNumber = null; 
                Uri contactData = data.getData(); 
                if (contactData == null) { 
                    return ; 
                } 
                Cursor cursor = managedQuery(contactData, null, null, null, null); 
                if (cursor.moveToFirst()) { 
//                  String name = cursor.getString(cursor 
//                          .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
                    String hasPhone = cursor 
                            .getString(cursor 
                                    .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
                    String id = cursor.getString(cursor 
                            .getColumnIndex(ContactsContract.Contacts._ID)); 
                    if (hasPhone.equalsIgnoreCase("1")) { 
                        hasPhone = "true"; 
                    } else { 
                        hasPhone = "false"; 
                    } 
                    if (Boolean.parseBoolean(hasPhone)) { 
                        Cursor phones = getContentResolver().query( 
                                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                                null, 
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID 
                              

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,