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

Android 開發:選擇聯絡人並返回電話號碼

編輯:關於 Android 程式設計

在跟隨某個教學實作時,它選取聯絡人的方式是自行從資料庫讀出聯絡人、再用 ListView 顯示,選擇後返回手機號碼。但這樣做有個問題:點擊選擇時需要等待載入,且顯示出來的聯絡人似乎並不齊全,整體體驗不佳。

於是筆者改用呼叫系統聯絡人的方式來選擇,省去自行處理界面的麻煩,成果相當不錯。

MainActivity

package jason.pickcontact;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
    Button pick;
    TextView show;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pick = (Button) findViewById(R.id.pick);
        show = (TextView) findViewById(R.id.show);
        pick.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // 呼叫系統聯絡人
                Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
                MainActivity.this.startActivityForResult(intent, 1);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    Uri contactData = data.getData();
                    Cursor cursor = managedQuery(contactData, null, null, null, null);
                    cursor.moveToFirst();
                    String num = this.getContactPhone(cursor);
                    show.setText("所選手機號為:" + num);
                }
                break;
        }
    }

    private String getContactPhone(Cursor cursor) {
        int phoneColumn = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
        int phoneNum = cursor.getInt(phoneColumn);
        String result = "";
        if (phoneNum > 0) {
            // 取得聯絡人的 ID
            int idColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID);
            String contactId = cursor.getString(idColumn);
            // 取得聯絡人電話的 cursor
            Cursor phone = getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                null,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId,
                null, null);
            if (phone.moveToFirst()) {
                for (; !phone.isAfterLast(); phone.moveToNext()) {
                    int index = phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                    String phoneNumber = phone.getString(index);
                    result = phoneNumber;
                }
                if (!phone.isClosed()) {
                    phone.close();
                }
            }
        }
        return result;
    }
}

關於 phone_type 的坑

聯絡人中儲存號碼的類型不同,對應的 phone_type 也不同。一般來說手機那一欄的類型是 2,但實測發現用 2 反而有些選不出來——查看聯絡人後才發現,很多人並沒有把號碼存到手機欄,而是存成了電話或其他類型。

實測結果顯示,不同系統的類型對應有些差異:MIUI 系統下,手機欄的 TYPE 是 2,住宅是 3 和 1,總機是 12 和 7,其他是 7;而在模擬器的原生系統上,mobile 是 2、home 是 1、work 是 3、other 是 7。可見不同 ROM 之間存在差別,不過手機為 2 應該是通用的。由於筆者儲存的類型不齊全,程式碼中暫且註解掉了類型判斷直接使用;標準做法應加上類型 2 的判斷較為嚴謹。

activity_main.xml

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
    <TextView
        android:id="@+id/show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <Button
        android:id="@+id/pick"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/show"
        android:layout_below="@+id/show"
        android:text="選擇聯絡人" />
</RelativeLayout>
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved