Pro Android with Kotlin
Download 5.42 Mb. Pdf ko'rish
|
@de android telegram Pro Android with Kotlin Developing Modern Mobile
160
CHAPTER 8: APIs ContactsContract.Data.RAW_CONTACT_ID, firstOperationIndex) // Sets the data row's MIME type to Email .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds. Email.CONTENT_ITEM_TYPE) // Email address and type .withValue(ContactsContract.CommonDataKinds. Email.ADDRESS, emailAddr) .withValue(ContactsContract.CommonDataKinds. Email.TYPE, android.provider.ContactsContract. CommonDataKinds.Email.TYPE_HOME) Finally, before closing the method, we add a yield point. This has no functional influence but introduces a break so the system can do other work to improve usability. The following snippet also contains the rest of the class: // Add a yield point. op.withYieldAllowed(true) opList.add(op.build()) } fun reset() { opList.clear() } fun doAll() { try { contentResolver.applyBatch( ContactsContract.AUTHORITY, opList as ArrayList) } catch (e: Exception) { // Display a warning val duration = Toast.LENGTH_SHORT val toast = Toast.makeText(ctx, "Something went wrong", duration) toast.show() // Log exception Log.e("LOG", "Exception encountered "+ "while inserting contact: " + e, e) } } } This uses a fixed phone type and a fixed e-mail type, but I guess you get the point. Also, make sure in productive code you use resource strings instead of hard-coded strings, as shown here. To use the class, all you have to do from inside an activity is the following: val cwr = ContactsWriter(this, contentResolver) cwr.addContact("com.google","user@gmail.com", "Peter","Kappa", 161 CHAPTER 8: APIs "post@kappa.com","0123456789") cwr.addContact("com.google","user@gmail.com", "Hilda","Kappa", "post2@kappa.com","0123456789") cwr.doAll() To update a contacts entry, we introduce another function inside the ContactsWriter class. fun updateContact(id:String, firstName:String?, lastName:String?, emailAddr:String?, phone:String?) { var op : ContentProviderOperation.Builder? = null if(firstName != null && lastName != null) { op = ContentProviderOperation.newUpdate( ContactsContract.Data.CONTENT_URI) .withSelection(ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?", arrayOf(id, ContactsContract.CommonDataKinds. StructuredName.CONTENT_ITEM_TYPE)) .withValue(ContactsContract.Contacts.DISPLAY_NAME, firstName + " " + lastName) opList.add(op.build()) } if(emailAddr != null) { op = ContentProviderOperation.newUpdate( ContactsContract.Data.CONTENT_URI) .withSelection(ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?", arrayOf(id, ContactsContract.CommonDataKinds. Email.CONTENT_ITEM_TYPE)) .withValue(ContactsContract.CommonDataKinds.Email. ADDRESS, emailAddr) opList.add(op.build()) } if(phone != null) { op = ContentProviderOperation.newUpdate( ContactsContract.Data.CONTENT_URI) .withSelection(ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?", arrayOf(id, ContactsContract.CommonDataKinds. Phone.CONTENT_ITEM_TYPE)) .withValue(ContactsContract.CommonDataKinds.Phone. NUMBER, phone) opList.add(op.build()) } } |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling