Created
July 31, 2020 03:17
-
-
Save bitristan/2f83b6edcc80279a6bf77972edb105dd to your computer and use it in GitHub Desktop.
读写通讯录
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity | |
import android.content.* | |
import android.database.Cursor | |
import android.provider.ContactsContract | |
import android.util.Log | |
object ContactUtil { | |
private const val TAG = "ContactUtil" | |
fun getContacts(context: Context): StringBuilder { | |
Log.d(TAG, "getContacts on thread ${Thread.currentThread().name}") | |
val builder = StringBuilder() | |
val resolver: ContentResolver = context.contentResolver | |
val optionalCursor = resolver.query( | |
ContactsContract.Contacts.CONTENT_URI, | |
null, null, null, null | |
) | |
if (optionalCursor == null) { | |
Log.d(TAG, "no contacts") | |
return StringBuilder() | |
} | |
Log.d(TAG, "count is ${optionalCursor.count}") | |
val cursor = optionalCursor | |
if (cursor.count > 0) { | |
while (cursor.moveToNext()) { | |
val id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)) | |
val name = | |
cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)) | |
val phoneNumber = (cursor.getString( | |
cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER) | |
)).toInt() | |
if (phoneNumber > 0) { | |
val cursorPhone = context.contentResolver.query( | |
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, | |
null, | |
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?", | |
arrayOf(id), | |
null | |
) as Cursor | |
if (cursorPhone.count > 0) { | |
while (cursorPhone.moveToNext()) { | |
val phoneNumValue = cursorPhone.getString( | |
cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER) | |
) | |
builder.append("Contact: ").append(name).append(", Phone Number: ") | |
.append( | |
phoneNumValue | |
).append("\n\n") | |
Log.d(TAG, "Name ===>$phoneNumValue") | |
} | |
} | |
cursorPhone.close() | |
} | |
} | |
} | |
cursor.close() | |
return builder | |
} | |
private fun addContactDirectly(context: Context) { | |
val values = ContentValues() | |
//首先向RawContacts.CONTENT_URI执行一个空值插入,目的是获取系统返回的rawContactId | |
val rawContactUri = context.contentResolver.insert(ContactsContract.RawContacts.CONTENT_URI, values) | |
?: return | |
val rawContactId = ContentUris.parseId(rawContactUri) | |
//往data表入姓名数据 | |
values.clear() | |
//添加id | |
values.put(ContactsContract.Contacts.Data.RAW_CONTACT_ID, rawContactId) | |
//添加内容类型(MIMETYPE) | |
values.put( | |
ContactsContract.Contacts.Data.MIMETYPE, | |
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE | |
) | |
//添加名字,添加到first name位置 | |
values.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, "测试姓名") | |
context.contentResolver.insert(ContactsContract.Data.CONTENT_URI, values) | |
//往data表入电话数据 | |
values.clear() | |
values.put(ContactsContract.Contacts.Data.RAW_CONTACT_ID, rawContactId) | |
values.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) | |
values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, "123456789") | |
values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) | |
context.contentResolver.insert(ContactsContract.Data.CONTENT_URI, values) | |
//往data表入Email数据 | |
values.clear() | |
values.put(ContactsContract.Contacts.Data.RAW_CONTACT_ID, rawContactId) | |
values.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE) | |
values.put(ContactsContract.CommonDataKinds.Email.DATA, "test123456@gmail.com") | |
values.put(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK) | |
context.contentResolver.insert(ContactsContract.Data.CONTENT_URI, values) | |
Log.d(TAG, "success write a contact ") | |
} | |
private fun addContactByActivity(activity: Activity) { | |
val name = "Test-Contact" | |
val phone = "1000001" | |
val intent = Intent(ContactsContract.Intents.Insert.ACTION) | |
intent.type = ContactsContract.RawContacts.CONTENT_TYPE | |
intent.putExtra(ContactsContract.Intents.Insert.NAME, name) | |
intent.putExtra(ContactsContract.Intents.Insert.PHONE, phone) | |
activity.startActivityForResult(intent, 1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment