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(
Do'stlaringiz bilan baham: |