Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android-調用本機的聯系人界面及返回 - 隨心

android-調用本機的聯系人界面及返回 - 隨心

編輯:關於Android編程

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

這裡寫兩個方法。

方法一:本人在小米手機測試可以,不過在其他的手機測試發現返回的Uri為空,不知為何,於是找到了第二種方法來解決。

跳轉至電話本
[java] www.2cto.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 
                                        + " = " + id, null, null); 
                        while (phones.moveToNext()) { 
                            phoneNumber = phones 
                                    .getString(phones 
                                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
                            setTitle(phoneNumber); 
                        } 
                        phones.close(); 
                    } 
                } 
                sendAdrress = phoneNumber; 
                SEND_TYPE = SEND_SMS_TYPE; 
                send_text_SMS.setText("發送短信" + " : " + phoneNumber); 
 
                break; 
            } 
            break; 
        } 
        super.onActivityResult(requestCode, resultCode, data); 
    } 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved