인텐트를 사용하여 연락처 수정

이 과정에서는 Intent를 사용하여 새 연락처를 삽입하거나 연락처 데이터를 수정하는 방법을 보여줍니다. Intent는 연락처 제공자에 직접 액세스하는 대신 연락처 앱을 시작하고, 이 앱은 적절한 Activity를 실행합니다. 이 과정에서 설명하는 수정 작업의 경우 Intent에서 확장 데이터를 전송하면 시작된 Activity의 UI에 입력됩니다.

Intent를 사용하여 단일 연락처를 삽입하거나 업데이트하는 것은 다음과 같은 이유로 연락처 제공자를 수정하는 데 선호되는 방법입니다.

  • 고유 UI 및 코드를 개발하는 데 소요되는 시간과 노력을 절약할 수 있습니다.
  • 연락처 제공자의 규칙을 따르지 않는 수정으로 인해 발생하는 오류를 방지합니다.
  • 요청해야 하는 권한의 수가 줄어듭니다. 앱은 연락처 제공자에 쓸 권한이 필요하지 않습니다. 이미 이 권한을 가지고 있는 연락처 앱에 연락처 수정을 위임하기 때문입니다.

인텐트를 사용하여 새 연락처 삽입

앱에서 새 데이터를 받으면 사용자가 새로운 연락처를 삽입하도록 허용하고 싶은 경우가 자주 있습니다. 예를 들어 음식점 리뷰 앱은 사용자가 음식점을 리뷰할 때 음식점을 연락처로 추가하도록 허용할 수 있습니다. 인텐트를 사용하여 이 작업을 실행하려면 사용 가능한 데이터를 최대한 많이 사용하여 인텐트를 만든 다음 연락처 앱으로 인텐트를 전송합니다.

연락처 앱을 사용하여 연락처를 삽입하면 연락처 제공자의 ContactsContract.RawContacts 테이블에 새 원시 연락처가 삽입됩니다. 필요한 경우 연락처 앱은 원시 연락처를 만들 때 사용할 계정 유형과 계정을 사용자에게 요청합니다. 또한 연락처 앱은 사용자에게 원시 연락처가 이미 있는지 여부를 알립니다. 그러면 사용자는 삽입을 취소할 수 있으며 이 경우 연락처가 생성되지 않습니다. 원시 연락처에 관한 자세한 내용은 연락처 제공자 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
}

자바

...
// 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
    )
}

자바

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)

자바

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

호출하면 연락처 앱에 화면이 열리며 사용자가 새로운 연락처를 입력할 수 있도록 허용합니다. 연락처의 계정 유형과 계정 이름이 화면 상단에 표시됩니다. 사용자가 데이터를 입력하고 완료를 클릭하면 연락처 앱의 연락처 목록이 표시됩니다. 사용자는 뒤로를 클릭하여 앱으로 돌아갑니다.

인텐트를 사용하여 기존 연락처 수정

사용자가 이미 관심 있는 연락처를 선택한 경우 Intent를 사용하여 기존 연락처를 수정하는 것이 유용합니다. 예를 들어 우편 주소는 있지만 우편번호가 없는 연락처를 찾는 앱은 사용자에게 우편번호를 조회한 후 연락처에 추가하는 옵션을 제공할 수 있습니다.

인텐트를 사용하여 기존 연락처를 수정하려면 연락처를 삽입하는 것과 유사한 절차를 사용합니다. 인텐트를 사용하여 새 연락처 삽입 섹션에 설명된 대로 인텐트를 만들지만 연락처의 Contacts.CONTENT_LOOKUP_URI 및 MIME 유형 Contacts.CONTENT_ITEM_TYPE를 인텐트에 추가합니다. 이미 가지고 있는 세부정보로 연락처를 수정하려면 인텐트의 확장 데이터에 세부정보를 넣으면 됩니다. 일부 이름 열은 인텐트를 사용하여 수정할 수 없습니다. 이러한 열은 ContactsContract.Contacts 클래스의 API 참조 요약 섹션에서 '업데이트' 제목 아래에 나열됩니다.

마지막으로 인텐트를 전송합니다. 연락처 앱에서 이에 대한 응답으로 수정 화면을 표시합니다. 사용자가 수정을 완료하고 수정을 저장하면 연락처 앱에 연락처 목록이 표시됩니다. 사용자가 뒤로를 클릭하면 앱이 표시됩니다.

인텐트 만들기

연락처를 수정하려면 Intent(action)를 호출하여 ACTION_EDIT 작업으로 인텐트를 만듭니다. setDataAndType()를 호출하여 인텐트의 데이터 값을 연락처의 Contacts.CONTENT_LOOKUP_URI로 설정하고 MIME 유형을 Contacts.CONTENT_ITEM_TYPE MIME 유형으로 설정합니다. setType()를 호출하면 Intent의 현재 데이터 값을 덮어쓰므로 데이터와 MIME 유형을 동시에 설정해야 합니다.

연락처의 Contacts.CONTENT_LOOKUP_URI를 가져오려면 연락처의 Contacts._ID 및 Contacts.LOOKUP_KEY 값을 인수로 사용하여 Contacts.getLookupUri(id, lookupkey)를 호출합니다.

참고: 연락처의 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)
    }

자바

    // 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 이전 버전에서는 이 키를 허용하지만 아무 영향을 미치지 않습니다. 확장 데이터를 설정하려면 다음을 수행합니다.

Kotlin

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

자바

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

다른 확장 데이터 추가

Intent에 확장 데이터를 추가하려면 필요에 따라 putExtra()를 호출합니다. Intents.Insert에 지정된 키 값을 사용하여 일반 연락처 필드의 확장 데이터를 추가할 수 있습니다. ContactsContract.Contacts 테이블의 일부 열은 수정할 수 없습니다. 이러한 열은 ContactsContract.Contacts 클래스의 API 참조 요약 섹션에 '업데이트'라는 제목으로 나열됩니다.

인텐트 전송

마지막으로, 구성한 인텐트를 전송합니다. 예를 들면 다음과 같습니다.

Kotlin

    // Sends the Intent
    startActivity(editIntent)

자바

    // 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)

자바

    // 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);