Pro Android with Kotlin


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

158
CHAPTER 8: APIs
As a third possibility, the URI given by ContactsContract.Contacts.CONTENT_FILTER_
URI allows for adding search criteria inside the URI instead of specifying them in the 
CursorLoader constructor.
...
val PROJECTION : Array? = null
val SELECTION : String? = null
val selectionArgs : Array? = null
val contentUri = Uri.withAppendedPath(
ContactsContract.Contacts.CONTENT_FILTER_URI,
Uri.encode(search))
Log.e("LOG", contentUri.toString())
...
Note that in this case it is not allowed to pass an empty string ("") as a search criterion.
Writing Contacts
Inserting or updating contacts best happens in batch mode. You start with a list of the item 
type ContentProviderOperation and fill it with operations as follows:
import android.content.Context
import android.content.ContentProviderOperation
import android.content.ContentResolver
import android.provider.ContactsContract
import android.content.ContentValues.TAG
import android.util.Log
import android.widget.Toast
class ContactsWriter(val ctx:Context, val contentResolver:
ContentResolver) {
val opList = mutableListOf()
fun addContact(accountType:String, accountName:String,
firstName:String, lastName:String,
emailAddr:String, phone:String) {
val firstOperationIndex = opList.size
Inside this method we first create a new contact. The Contacts table will be filled 
automatically; direct access is not possible anyway. The device’s user account and account 
type are needed; otherwise, the operations silently will fail!
// Creates a new raw contact.
var op = ContentProviderOperation.newInsert(
ContactsContract.RawContacts.CONTENT_URI)
.withValue(
ContactsContract.RawContacts.ACCOUNT_TYPE,
accountType)
.withValue(



Download 5.42 Mb.

Do'stlaringiz bilan baham:
1   ...   134   135   136   137   138   139   140   141   ...   223




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