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

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

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

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

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

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

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

इंटेंट बनाएं

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

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

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

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

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

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

इंटेंट बनाना

किसी संपर्क में बदलाव करने के लिए, 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._ID और Contacts.LOOKUP_KEY मान इस रूप में आर्ग्युमेंट.

ध्यान दें: किसी संपर्क की 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) और उसके बाद के वर्शन में इस समस्या को हल करने के लिए, true की वैल्यू के साथ, इंटेंट में एक्सटेंडेड डेटा बटन finishActivityOnSaveCompleted जोड़ें. 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);

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

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

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