इंटेंट का इस्तेमाल करके संपर्कों में बदलाव करें

इस लेसन में, नया संपर्क जोड़ने या किसी संपर्क के डेटा में बदलाव करने के लिए, Intent का इस्तेमाल करने का तरीका बताया गया है. संपर्क सूची को सीधे ऐक्सेस करने के बजाय, Intent संपर्क ऐप्लिकेशन को चालू करता है. यह ऐप्लिकेशन, सही Activity को चलाता है. इस लेसन में बताए गए बदलावों के लिए, अगर Intent में ज़्यादा डेटा भेजा जाता है, तो उसे शुरू किए गए Activity के यूज़र इंटरफ़ेस (यूआई) में डाला जाता है.

संपर्क सूची में बदलाव करने के लिए, Intent का इस्तेमाल करके किसी एक संपर्क को जोड़ना या अपडेट करना सबसे सही तरीका है. इसकी वजहें यहां दी गई हैं:

  • इससे आपको अपना यूज़र इंटरफ़ेस और कोड डेवलप करने में लगने वाला समय और मेहनत बचती है.
  • इससे, संपर्क सूची के नियमों का पालन न करने वाले बदलावों की वजह से होने वाली गड़बड़ियों से बचा जा सकता है.
  • इससे आपको कम अनुमतियों का अनुरोध करना पड़ता है. आपके ऐप्लिकेशन को संपर्क सूची में बदलाव करने की अनुमति की ज़रूरत नहीं है. ऐसा इसलिए, क्योंकि यह संपर्क ऐप्लिकेशन में बदलाव करने की अनुमति देता है. इस ऐप्लिकेशन के पास पहले से ही यह अनुमति होती है.

इंटेंट का इस्तेमाल करके नया संपर्क जोड़ना

जब आपके ऐप्लिकेशन को नया डेटा मिलता है, तो अक्सर आपको उपयोगकर्ता को नया संपर्क जोड़ने की अनुमति देनी होती है. उदाहरण के लिए, रेस्टोरेंट की समीक्षा करने वाला कोई ऐप्लिकेशन, उपयोगकर्ताओं को रेस्टोरेंट की समीक्षा करते समय उसे संपर्क के तौर पर जोड़ने की अनुमति दे सकता है. इंटेंट का इस्तेमाल करके ऐसा करने के लिए, उपलब्ध डेटा का इस्तेमाल करके इंटेंट बनाएं. इसके बाद, इंटेंट को संपर्क ऐप्लिकेशन पर भेजें.

संपर्क ऐप्लिकेशन का इस्तेमाल करके संपर्क डालने पर, संपर्क सूची के ContactsContract.RawContacts टेबल में एक नया रॉ संपर्क जुड़ जाता है. ज़रूरत पड़ने पर, संपर्क ऐप्लिकेशन उपयोगकर्ताओं से रॉ कॉन्टैक्ट बनाते समय इस्तेमाल किए जाने वाले खाते के टाइप और खाते के बारे में पूछता है. अगर रॉ कॉन्टैक्ट पहले से मौजूद है, तो संपर्क ऐप्लिकेशन उपयोगकर्ताओं को इसकी सूचना भी देता है. इसके बाद, उपयोगकर्ताओं के पास संपर्क जोड़ने की प्रोसेस को रद्द करने का विकल्प होता है. ऐसा करने पर, कोई संपर्क नहीं जोड़ा जाता. रॉ कॉन्टैक्ट के बारे में ज़्यादा जानने के लिए, संपर्क सूची एपीआई की गाइड देखें.

इंटेंट बनाना

शुरू करने के लिए, Intents.Insert.ACTION कार्रवाई के साथ एक नया Intent ऑब्जेक्ट बनाएं. एमआईएमई टाइप को 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);

इस कॉल से, संपर्क ऐप्लिकेशन में एक स्क्रीन खुलती है. इससे उपयोगकर्ता नया संपर्क जोड़ सकते हैं. संपर्क के लिए खाता टाइप और खाते का नाम, स्क्रीन पर सबसे ऊपर दिखता है. जब उपयोगकर्ता डेटा डालते हैं और हो गया पर क्लिक करते हैं, तो संपर्क ऐप्लिकेशन की संपर्क सूची दिखती है. उपयोगकर्ता, वापस जाएं पर क्लिक करके आपके ऐप्लिकेशन पर वापस आते हैं.

इंटेंट का इस्तेमाल करके किसी मौजूदा संपर्क की जानकारी में बदलाव करना

अगर उपयोगकर्ता ने पहले से ही कोई संपर्क चुना हुआ है, तो Intent का इस्तेमाल करके किसी मौजूदा संपर्क में बदलाव करना मददगार होता है. उदाहरण के लिए, कोई ऐप्लिकेशन ऐसे संपर्कों को ढूंढता है जिनके पास डाक पते हैं, लेकिन पिन कोड नहीं है. ऐसे में, वह उपयोगकर्ताओं को कोड खोजने और फिर उसे संपर्क में जोड़ने का विकल्प दे सकता है.

इंटेंट का इस्तेमाल करके किसी मौजूदा संपर्क में बदलाव करने के लिए, संपर्क जोड़ने जैसी ही प्रोसेस अपनाएं. इंटेंट का इस्तेमाल करके नया संपर्क जोड़ना सेक्शन में बताए गए तरीके से इंटेंट बनाएं. हालांकि, इंटेंट में संपर्क का Contacts.CONTENT_LOOKUP_URI और एमआईएमई टाइप Contacts.CONTENT_ITEM_TYPE जोड़ें. अगर आपको पहले से मौजूद जानकारी की मदद से संपर्क में बदलाव करना है, तो उसे इंटेंट के एक्सटेंडेड डेटा में डाला जा सकता है. ध्यान दें कि कुछ नाम वाले कॉलम में, इंटेंट का इस्तेमाल करके बदलाव नहीं किया जा सकता. इन कॉलम को ContactsContract.Contacts क्लास के लिए, एपीआई का संदर्भ के खास जानकारी वाले सेक्शन में, "अपडेट करें" हेडिंग के तहत लिस्ट किया गया है.

आखिर में, इंटेंट भेजें. इसके बाद, संपर्क ऐप्लिकेशन में बदलाव करने वाली स्क्रीन दिखती है. जब उपयोगकर्ता बदलाव कर लेता है और उन्हें सेव कर लेता है, तब संपर्क ऐप्लिकेशन में संपर्क सूची दिखती है. जब उपयोगकर्ता वापस जाएं पर क्लिक करता है, तो आपका ऐप्लिकेशन दिखता है.

इंटेंट बनाना

किसी संपर्क में बदलाव करने के लिए, Intent(action) को कॉल करें. इससे ACTION_EDIT कार्रवाई के साथ इंटेंट बनाया जा सकेगा. कॉल setDataAndType(), इंटेंट के लिए डेटा वैल्यू को संपर्क के Contacts.CONTENT_LOOKUP_URI पर और एमआईएमई टाइप को Contacts.CONTENT_ITEM_TYPE एमआईएमई टाइप पर सेट करने के लिए; क्योंकि setType() को किया गया कॉल, Intent के लिए मौजूदा डेटा वैल्यू को ओवरराइट कर देता है, इसलिए आपको डेटा और एमआईएमई टाइप को एक ही समय पर सेट करना होगा.

किसी संपर्क का Contacts.CONTENT_LOOKUP_URI पाने के लिए, संपर्क के Contacts._ID और Contacts.LOOKUP_KEY वैल्यू को आर्ग्युमेंट के तौर पर इस्तेमाल करके, Contacts.getLookupUri(id, lookupkey) को कॉल करें.

ध्यान दें: संपर्क की LOOKUP_KEY वैल्यू, वह आइडेंटिफ़ायर है जिसका इस्तेमाल करके संपर्क को वापस पाया जा सकता है. यह आईडी हमेशा एक जैसा रहता है, भले ही प्रोवाइडर इंटरनल ऑपरेशन मैनेज करने के लिए, संपर्क की लाइन का आईडी बदल दे.

यहां दिए गए स्निपेट में, इंटेंट बनाने का तरीका बताया गया है:

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 (एपीआई वर्शन 14) और इसके बाद के वर्शन में, संपर्क ऐप्लिकेशन में मौजूद किसी समस्या की वजह से, गलत नेविगेशन होता है. जब आपका ऐप्लिकेशन, संपर्क ऐप्लिकेशन को बदलाव करने का इंटेंट भेजता है और उपयोगकर्ता किसी संपर्क में बदलाव करके उसे सेव करते हैं, तो वापस जाएं पर क्लिक करने पर उन्हें संपर्क सूची वाली स्क्रीन दिखती है. आपके ऐप्लिकेशन पर वापस जाने के लिए, उन्हें हाल ही में इस्तेमाल किए गए ऐप्लिकेशन पर क्लिक करके, आपका ऐप्लिकेशन चुनना होगा.

Android 4.0.3 (एपीआई वर्शन 15) और इसके बाद के वर्शन में इस समस्या को हल करने के लिए, इंटेंट में ज़्यादा डेटा वाली कुंजी finishActivityOnSaveCompleted जोड़ें. साथ ही, इसकी वैल्यू true पर सेट करें. Android 4.0 से पहले के वर्शन में यह कुंजी स्वीकार की जाती है, लेकिन इसका कोई असर नहीं होता. ज़्यादा डेटा सेट करने के लिए, यह तरीका अपनाएं:

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 के लिए एपीआई रेफ़रंस के खास जानकारी वाले सेक्शन में, "अपडेट करें" हेडिंग के नीचे दिए गए हैं.

इंटेंट भेजें

आखिर में, बनाए गए इंटेंट को भेजें. उदाहरण के लिए:

Kotlin

    // Sends the Intent
    startActivity(editIntent)

Java

    // Sends the Intent
    startActivity(editIntent);

उपयोगकर्ताओं को इंटेंट का इस्तेमाल करके, डेटा डालने या उसमें बदलाव करने की अनुमति देना

उपयोगकर्ताओं को यह चुनने की अनुमति दी जा सकती है कि उन्हें कोई संपर्क जोड़ना है या किसी मौजूदा संपर्क में बदलाव करना है. इसके लिए, ACTION_INSERT_OR_EDIT कार्रवाई के साथ Intent भेजें. उदाहरण के लिए, ईमेल क्लाइंट ऐप्लिकेशन, उपयोगकर्ताओं को किसी नए संपर्क में इनकमिंग ईमेल पता जोड़ने या किसी मौजूदा संपर्क के लिए अतिरिक्त पते के तौर पर जोड़ने की अनुमति दे सकता है. इस इंटेंट के लिए, एमआईएमई टाइप को Contacts.CONTENT_ITEM_TYPE पर सेट करें, लेकिन डेटा यूआरआई सेट न करें.

यह इंटेंट भेजने पर, संपर्क ऐप्लिकेशन संपर्कों की सूची दिखाता है. उपयोगकर्ता, नया संपर्क जोड़ सकते हैं या किसी मौजूदा संपर्क को चुनकर उसमें बदलाव कर सकते हैं. इंटेंट में जोड़े गए सभी एक्सटेंडेड डेटा फ़ील्ड, स्क्रीन पर दिखते हैं. 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);