android 如何開啟系統連絡人介面

來源:互聯網
上載者:User

在android應用程式的開發中,經常要實現的一個功能是調用系統內建的連絡人介面,做到選擇人之後,取得相應的名稱和號碼,並返回。

    android2.0前後實現方式上也有一定的差別:主要是2.0及以上版本連絡人API發生了變化,由原來的Contacts變成了ContactsContract。

(一) 2.0之前取連絡人清單的方式聲明意圖方式一Intent intent = new Intent();intent.setAction(Intent.ACTION_PICK);intent.setData(Contacts.People.CONTENT_URI);startActivityForResult(intent,  

PICK_CONTACT

 

);聲明意圖方式二Intent intent  = new Intent(Intent.ACTION_PICK);intent.setType("Contacts.People.CONTENT_TYPE");//vnd.android.cursor.dir/personstartActivityForResult(intent,  

PICK_CONTACT

 

);實現onActivityResultprotected void onActivityResult (int requestCode, int resultCode, Intent data) {        super.onActivityResult(requestCode, resultCode, data);        switch (requestCode) {            case  

PICK_CONTACT

 

:                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));                Log.d(TAG, "number" + number);                mContactText.setText(number);                mContactText.setSelection(number.length());                break;                        default:                break;        }    } 
(二) 2.0之後取連絡人清單的方式聲明意圖方式一

Intent intent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);startActivityForResult(intent, PICK_CONTACT); 聲明意圖方式二 Intent intent = new Intent(Intent.ACTION_PICK); 

      intent.setType(ContactsContract.Contacts.CONTENT_TYPE);//vnd.android.cursor.dir/contactstartActivityForResult(intent, PICK_CONTACT); 實現onActivityResult@Overridepublic void onActivityResult(int reqCode, int resultCode, Intent data) {  super.onActivityResult(reqCode, resultCode, data);
  switch (reqCode) {    case (PICK_CONTACT) :      if (resultCode == Activity.RESULT_OK) {        Uri contactData = data.getData();        Cursor c =  managedQuery(contactData, null, null, null, null);        if (c.moveToFirst()) {          String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));          String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));          String phoneNumber = null;          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 +" = "+ contactId,null, null);           while (phones.moveToNext()) {             phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));           }           phones.close();          }        }      }      break;  }}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.