使用意圖修改聯絡人

本課程將說明如何使用 Intent 插入新聯絡人或修改聯絡人的資料。Intent 不會直接存取聯絡人供應程式,而是啟動聯絡人應用程式,以執行適當的 Activity。針對本課程所述的修改動作,如果您透過 Intent 傳送擴充資料,則系統會將這些資料輸入到已開始的 Activity 使用者介面中。

如要修改聯絡人供應程式,建議使用 Intent 插入或更新單一聯絡人,原因如下:

  • 節省您自行開發 UI 和程式碼所需的時間和心力。
  • 可以避免因修改不符合聯絡人提供者規則而造成的錯誤。
  • 這樣可以減少您需要要求的權限數量。應用程式不需要寫入聯絡人供應程式的權限,因為這類應用程式會委派聯絡人應用程式 (具有這項權限)。

使用意圖插入新聯絡人

您經常希望在應用程式收到新資料時,允許使用者插入新聯絡人。例如,餐廳評論應用程式可讓使用者將正在評論的餐廳新增為聯絡人。如要使用意圖達成此目的,請盡可能使用手上的資料建立意圖,然後將意圖傳送至聯絡人應用程式。

使用「聯絡人」應用程式插入聯絡人時,系統會將新的「原始」聯絡人插入聯絡人提供者的 ContactsContract.RawContacts 資料表。如有需要,聯絡人應用程式會提示使用者在建立原始聯絡人時,輸入帳戶類型和帳戶。此外,聯絡人應用程式也會通知使用者是否已有原始聯絡人。使用者可以選擇取消插入作業,這樣就不會建立任何聯絡人。如要進一步瞭解原始聯絡人,請參閱 Contacts Provider API 指南。

建立意圖

首先,請使用動作 Intents.Insert.ACTION 建立新的 Intent 物件。將 MIME 類型設為 RawContacts.CONTENT_TYPE。例如:

Kotlin

...
// Creates a new Intent to insert a contact
val intent = Intent(ContactsContract.Intents.Insert.ACTION).apply {
    // Sets the MIME type to match the Contacts Provider
    type = ContactsContract.RawContacts.CONTENT_TYPE
}

Java

...
// Creates a new Intent to insert a contact
Intent intent = new Intent(ContactsContract.Intents.Insert.ACTION);
// Sets the MIME type to match the Contacts Provider
intent.setType(ContactsContract.RawContacts.CONTENT_TYPE);

如果已有聯絡人的詳細資料 (例如電話號碼或電子郵件地址),可以將這些資料插入意圖做為擴充資料。至於鍵/值,請使用 Intents.Insert 提供的適當常數。聯絡人應用程式會在插入畫面中顯示資料,讓使用者可以進一步編輯及新增資料。

Kotlin

private var emailAddress: EditText? = null
private var phoneNumber: EditText? = null
...
/* Assumes EditText fields in your UI contain an email address
 * and a phone number.
 *
 */
emailAddress = findViewById(R.id.email)
phoneNumber = findViewById(R.id.phone)
...
/*
 * Inserts new data into the Intent. This data is passed to the
 * contacts app's Insert screen
 */
intent.apply {
    // Inserts an email address
    putExtra(ContactsContract.Intents.Insert.EMAIL, emailAddress?.text)
    /*
     * In this example, sets the email type to be a work email.
     * You can set other email types as necessary.
     */
    putExtra(
            ContactsContract.Intents.Insert.EMAIL_TYPE,
            ContactsContract.CommonDataKinds.Email.TYPE_WORK
    )
    // Inserts a phone number
    putExtra(ContactsContract.Intents.Insert.PHONE, phoneNumber?.text)
    /*
     * In this example, sets the phone type to be a work phone.
     * You can set other phone types as necessary.
     */
    putExtra(
            ContactsContract.Intents.Insert.PHONE_TYPE,
            ContactsContract.CommonDataKinds.Phone.TYPE_WORK
    )
}

Java

private EditText emailAddress = null;
private EditText phoneNumber = null;
...
/* Assumes EditText fields in your UI contain an email address
 * and a phone number.
 *
 */
emailAddress = (EditText) findViewById(R.id.email);
phoneNumber = (EditText) findViewById(R.id.phone);
...
/*
 * Inserts new data into the Intent. This data is passed to the
 * contacts app's Insert screen
 */
// Inserts an email address
intent.putExtra(ContactsContract.Intents.Insert.EMAIL, emailAddress.getText())
/*
 * In this example, sets the email type to be a work email.
 * You can set other email types as necessary.
 */
      .putExtra(ContactsContract.Intents.Insert.EMAIL_TYPE,
            ContactsContract.CommonDataKinds.Email.TYPE_WORK)
// Inserts a phone number
      .putExtra(ContactsContract.Intents.Insert.PHONE, phoneNumber.getText())
/*
 * In this example, sets the phone type to be a work phone.
 * You can set other phone types as necessary.
 */
      .putExtra(ContactsContract.Intents.Insert.PHONE_TYPE,
            ContactsContract.CommonDataKinds.Phone.TYPE_WORK);

建立 Intent 後,請呼叫 startActivity() 來傳送。

Kotlin

    /* Sends the Intent
     */
    startActivity(intent)

Java

    /* Sends the Intent
     */
    startActivity(intent);

這場通話會在聯絡人應用程式中開啟,可讓使用者輸入新聯絡人。螢幕頂端會列出聯絡人的帳戶類型和帳戶名稱。只要使用者輸入資料並按一下「完成」,系統便會顯示聯絡人應用程式的聯絡人清單。使用者按一下「Back」返回應用程式。

使用意圖編輯現有聯絡人

如果使用者已選擇關注的聯絡人,使用 Intent 編輯現有聯絡人就非常實用。舉例來說,如果應用程式尋找的聯絡人有郵寄地址,但缺少郵遞區號,使用者就能選擇查詢代碼並將代碼新增至聯絡人。

如要使用意圖編輯現有聯絡人,請按照類似插入聯絡人的程序操作。按照「使用意圖插入新聯絡人」一節所述建立意圖,但請將聯絡人的 Contacts.CONTENT_LOOKUP_URI 和 MIME 類型 Contacts.CONTENT_ITEM_TYPE 加入意圖。如要編輯具有現有詳細資料的聯絡人,可將這些資料放入意圖的擴充資料中。請注意,部分名稱欄無法使用意圖編輯;這些資料欄會列在 ContactsContract.Contacts 類別 API 參考資料的摘要部分,位於「Update」標題下方。

最後,傳送意圖。為回應,「聯絡人」應用程式會顯示編輯畫面。使用者完成編輯並儲存編輯內容後,「聯絡人」應用程式就會顯示聯絡人清單。當使用者按一下「Back」時,系統便會顯示應用程式。

建立意圖

如要編輯聯絡人,請呼叫 Intent(action) 以建立含有 ACTION_EDIT 動作的意圖。呼叫 setDataAndType() 以將意圖的資料值設定為聯絡人的 Contacts.CONTENT_LOOKUP_URI,並且將 MIME 類型設為 Contacts.CONTENT_ITEM_TYPE MIME 類型。由於呼叫 setType() 會覆寫 Intent 目前的資料值,因此您必須同時設定資料和 MIME 類型。

如要取得聯絡人的 Contacts.CONTENT_LOOKUP_URI,請呼叫 Contacts.getLookupUri(id, lookupkey) 並將聯絡人的 Contacts._IDContacts.LOOKUP_KEY 值做為引數。

注意:聯絡人的 LOOKUP_KEY 值是您擷取聯絡人時應使用的 ID。即使提供者變更聯絡人的資料列 ID 來處理內部作業,此屬性也會保持不變。

下列程式碼片段說明如何建立意圖:

Kotlin

    // The Cursor that contains the Contact row
    var mCursor: Cursor? = null
    // The index of the lookup key column in the cursor
    var lookupKeyIndex: Int = 0
    // The index of the contact's _ID value
    var idIndex: Int = 0
    // The lookup key from the Cursor
    var currentLookupKey: String? = null
    // The _ID value from the Cursor
    var currentId: Long = 0
    // A content URI pointing to the contact
    var selectedContactUri: Uri? = null
    ...
    /*
     * Once the user has selected a contact to edit,
     * this gets the contact's lookup key and _ID values from the
     * cursor and creates the necessary URI.
     */
    mCursor?.apply {
        // Gets the lookup key column index
        lookupKeyIndex = getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)
        // Gets the lookup key value
        currentLookupKey = getString(lookupKeyIndex)
        // Gets the _ID column index
        idIndex = getColumnIndex(ContactsContract.Contacts._ID)
        currentId = getLong(idIndex)
        selectedContactUri = ContactsContract.Contacts.getLookupUri(currentId, mCurrentLookupKey)
    }

    // Creates a new Intent to edit a contact
    val editIntent = Intent(Intent.ACTION_EDIT).apply {
        /*
         * Sets the contact URI to edit, and the data type that the
         * Intent must match
         */
        setDataAndType(selectedContactUri, ContactsContract.Contacts.CONTENT_ITEM_TYPE)
    }

Java

    // The Cursor that contains the Contact row
    public Cursor mCursor;
    // The index of the lookup key column in the cursor
    public int lookupKeyIndex;
    // The index of the contact's _ID value
    public int idIndex;
    // The lookup key from the Cursor
    public String currentLookupKey;
    // The _ID value from the Cursor
    public long currentId;
    // A content URI pointing to the contact
    Uri selectedContactUri;
    ...
    /*
     * Once the user has selected a contact to edit,
     * this gets the contact's lookup key and _ID values from the
     * cursor and creates the necessary URI.
     */
    // Gets the lookup key column index
    lookupKeyIndex = mCursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY);
    // Gets the lookup key value
    currentLookupKey = mCursor.getString(lookupKeyIndex);
    // Gets the _ID column index
    idIndex = mCursor.getColumnIndex(ContactsContract.Contacts._ID);
    currentId = mCursor.getLong(idIndex);
    selectedContactUri =
            Contacts.getLookupUri(currentId, mCurrentLookupKey);
    ...
    // Creates a new Intent to edit a contact
    Intent editIntent = new Intent(Intent.ACTION_EDIT);
    /*
     * Sets the contact URI to edit, and the data type that the
     * Intent must match
     */
    editIntent.setDataAndType(selectedContactUri, ContactsContract.Contacts.CONTENT_ITEM_TYPE);

新增導覽旗標

在 Android 4.0 (API 版本 14) 以上版本中,聯絡人應用程式發生問題會導致瀏覽錯誤。當應用程式將編輯意圖傳送至「聯絡人」應用程式,且使用者編輯和儲存聯絡人時,當使用者按一下「返回」時,就會看到聯絡人清單畫面。如要返回應用程式,使用者必須按一下「最近」並選擇您的應用程式。

如要在 Android 4.0.3 (API 版本 15) 以上版本中解決這個問題,請將擴充資料鍵 finishActivityOnSaveCompleted 新增至意圖,並將值設為 true。Android 4.0 以下版本的 Android 版本接受這個金鑰,但沒有任何作用。如要設定擴充資料,請執行下列操作:

Kotlin

    // Sets the special extended data for navigation
    editIntent.putExtra("finishActivityOnSaveCompleted", true)

Java

    // Sets the special extended data for navigation
    editIntent.putExtra("finishActivityOnSaveCompleted", true);

新增其他延伸資料

如要在 Intent 中新增其他延伸資料,請視需要呼叫 putExtra()。您可以使用 Intents.Insert 中指定的鍵值,為常見的聯絡人欄位新增延伸資料。請記住,ContactsContract.Contacts 資料表中的部分資料欄無法修改。這些資料欄會列在 ContactsContract.Contacts 類別 API 參考資料的摘要部分,位於「Update」標題下方。

傳送意圖

最後,傳送建構的意圖。例如:

Kotlin

    // Sends the Intent
    startActivity(editIntent)

Java

    // Sends the Intent
    startActivity(editIntent);

讓使用者選擇使用意圖插入或編輯

您可以傳送含有 ACTION_INSERT_OR_EDIT 動作的 Intent,讓使用者選擇要插入聯絡人或編輯現有聯絡人。舉例來說,電子郵件用戶端應用程式可讓使用者將傳入的電子郵件地址新增至新聯絡人,或是新增為現有聯絡人的其他地址。請將這個意圖的 MIME 類型設為 Contacts.CONTENT_ITEM_TYPE,但不要設定資料 URI。

傳送此意圖時,「聯絡人」應用程式會顯示聯絡人清單。使用者可以插入新聯絡人,或是選擇現有聯絡人進行編輯。 任何新增至意圖的擴充資料欄位都會填入要顯示的畫面。您可以使用 Intents.Insert 中指定的任何鍵值。下列程式碼片段說明如何建構並傳送意圖:

Kotlin

    // Creates a new Intent to insert or edit a contact
    val intentInsertEdit = Intent(Intent.ACTION_INSERT_OR_EDIT).apply {
        // Sets the MIME type
        type = ContactsContract.Contacts.CONTENT_ITEM_TYPE
    }
    // Add code here to insert extended data, if desired

    // Sends the Intent with an request ID
    startActivity(intentInsertEdit)

Java

    // Creates a new Intent to insert or edit a contact
    Intent intentInsertEdit = new Intent(Intent.ACTION_INSERT_OR_EDIT);
    // Sets the MIME type
    intentInsertEdit.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
    // Add code here to insert extended data, if desired
    ...
    // Sends the Intent with an request ID
    startActivity(intentInsertEdit);