編輯:關於 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
以上就是本文的全部內容,希望對你的學習有所幫助。
Android JNI 入門完整實例:從靜態註冊到動態註冊
一、JNI 是什麼 JNI 是 Java Native Interface 的縮寫,中文叫「Java 本地調用」,是一套本地程式設計介面。它讓跑在 Java 虛擬機裡的
深入解析 Android 的 px、dp 與 sp 三種單位
自 Android 生態興起以來,開發者人數持續攀升,市面上的機型也越來越琳瑯滿目。設備尺寸、解析度、像素密度千差萬別,再加上各家廠商深度定製的 ROM,使得 Andro
Android 5.0 共享元素轉場動畫實戰:從 RecyclerView 到詳情頁的平滑過渡
Android 5.0 共享元素轉場動畫實戰 Android 5.0 以後,Activity 轉場動畫變得相當容易。只要使用 ActivityOptionsCompat
Z Launcher 怎麼用?Nokia Z Launcher 桌面介紹
下載安裝好應用後,首次打開會出現隱私許可協議,點擊接受後便進入教學頁面,選擇跳過教學即可進入主頁面。可以看到它非常簡潔,整個 Launcher 只有主螢幕與應用程式列表,