Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 讀取聯絡人姓名與電話號碼的實作程式碼

編輯:關於 Android 程式設計

開發中常有取得聯絡人列表的需求,但這次要做的不是列出整個通訊錄,而是在使用者點選單一聯絡人之後,取出該聯絡人的姓名與電話,並自動填入指定的輸入框,省去手動輸入的麻煩。以下是實作程式碼。

版面配置

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="40dp" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom" android:paddingLeft="10dp" android:text="姓名:" android:textColor="@android:color/black" android:textSize="13sp" /> <EditText android:id="@+id/et_name" android:layout_width="200dp" android:layout_height="fill_parent" android:layout_marginLeft="10dp" /> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="40dp" android:text="點擊" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="40dp" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom" android:paddingLeft="10dp" android:text="電話:" android:textColor="@android:color/black" android:textSize="13sp" /> <EditText android:id="@+id/et_phone" android:layout_width="200dp" android:layout_height="fill_parent" android:layout_marginLeft="10dp" android:inputType="phone" /> </LinearLayout> </LinearLayout> 

這是一個很普通的版面配置檔。

主要功能程式碼

 /** * 獲取聯系人電話 * * @param cursor * @param context * @return */ private ContactBen getContactPhone(Cursor cursor, Context context) { ContactBen vo = new ContactBen(); int phoneColumn = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER); int phoneNum = 0; try { phoneNum = cursor.getInt(phoneColumn); } catch (Exception e) { return null; } // String phoneResult = ""; if (phoneNum > 0) { // 獲得聯系人的ID號 int idColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID); String contactId = cursor.getString(idColumn); vo.name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); // 獲得聯系人的電話號碼的cursor; Cursor phones = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null); if (phones.moveToFirst()) { // 遍歷所有的電話號碼 for (; !phones.isAfterLast(); phones.moveToNext()) { int index = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); int typeindex = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE); int phone_type = phones.getInt(typeindex); String phoneNumber = phones.getString(index); switch (phone_type) { case 2: vo.phone = phoneNumber; break; } } if (!phones.isClosed()) { phones.close(); } } } return vo; } 

這裡有一個關鍵細節:務必包上 try catch。因為 Android 手機會把微信、QQ 等應用產生的聯絡方式也一併列入通訊錄,而這些項目其實沒有電話號碼,按下返回時便取不到值,若沒有例外處理就會直接崩潰。另外 phone_type 為 2 代表手機號碼,可依需求再擴充其他類型(例如 1 為住家、3 為工作)。

取得結果的回呼

 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case (1): { if (resultCode == Activity.RESULT_OK) { if (data != null) { Uri contactData = data.getData(); @SuppressWarnings("deprecation") Cursor c = MainActivity.this.managedQuery(contactData, null, null, null, null); c.moveToFirst(); ContactBen contactPhone = getContactPhone(c, MainActivity.this); if (contactPhone == null) { contactPhone = new ContactBen(); } et_name.setText("" + contactPhone.name); et_phone.setText("" + contactPhone.phone); } } break; } } }

在這個回呼中就能取到所需的資料,並直接填入輸入框。

點擊事件與權限申請

 findViewById(R.id.btn1).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { requestPermission(new String[] { Manifest.permission.READ_CONTACTS }, new PermissionHandler() { @Override public void onGranted() { Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); startActivityForResult(intent, 1); } @Override public void onDenied() { super.onDenied(); } }); } });

這裡已經針對 Android 6.0 及以上版本做了動態權限適配。最後別忘了在清單檔中加入 READ_CONTACTS 的權限宣告。

最終效果如下:

原始碼位址:contactperson

以上就是本文的全部內容,希望對你的學習有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved