Pro Android with Kotlin


Download 5.42 Mb.
Pdf ko'rish
bet141/223
Sana04.11.2023
Hajmi5.42 Mb.
#1746807
1   ...   137   138   139   140   141   142   143   144   ...   223
Bog'liq
@de android telegram Pro Android with Kotlin Developing Modern Mobile

162
CHAPTER 8: APIs
As an input, you need the ID key from the Raw Contacts table; any function argument not 
null gets updated. For example, you can write the following inside the activity:
val rawId = ...
val cwr = ContactsWriter(this, contentResolver)
cwr.updateContact(rawId, null, null,
"postXXX@kappa.com", null)
cwr.doAll()
As a last function, we add the possibility to delete a contact, again based on the ID.
fun delete(id:String) {
var op = ContentProviderOperation.newDelete(
ContactsContract.RawContacts.CONTENT_URI)
.withSelection(ContactsContract.RawContacts.
CONTACT_ID + " = ?",
arrayOf(id))
opList.add(op.build())
}
Using it is similar to an update.
val rawId = ...
val cwr = ContactsWriter(this, contentResolver)
cwr.delete(rawId)
cwr.doAll()
Using Contacts System Activities
To read or update a single contact, you can avoid having to write your own user interface. 
Just use the system activity to access a contact. An appropriate intent call for creating a 
single contact looks like this:
val intent = Intent(Intents.Insert.ACTION)
intent.setType(ContactsContract.RawContacts.CONTENT_TYPE)
intent.putExtra(Intents.Insert.EMAIL, emailAddress)
.putExtra(Intents.Insert.EMAIL_TYPE,
CommonDataKinds.Email.TYPE_WORK)
.putExtra(Intents.Insert.PHONE, phoneNumber)
.PutExtra(Intents.Insert.PHONE_TYPE, Phone.
TYPE_WORK)
startActivity(intent)
This will open the contacts screen for creating a new contact and prefill the given fields.
To instead edit an existing contact, once you have the lookup key and the raw contact ID, as 
shown earlier, write the following:
val uri = Contacts.getLookupUri(id, lookupKey)
val intent = Intent(Intent.ACTION_EDIT)
// the following must be done in _one_ call, do not
// chain .setData() and .setType(), because they
// overwrite each other!



Download 5.42 Mb.

Do'stlaringiz bilan baham:
1   ...   137   138   139   140   141   142   143   144   ...   223




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling