Table of contents
read contacts
Android can read the contact table through contentResolver. The Uri information of the contact table is: content://com.android.contacts/data/phones to output the contact information.
Relevant permissions are required:
<uses-permission android:name="android.permission.READ_CONTACTS">
</uses-permission>
The specific code is as follows:
//需要读联系人权限READ_CONTACTS
@SuppressLint("Range")
private fun getContact() {
//查询raw_contacts表获得联系人
val resolver = contentResolver
val uri = Uri.parse("content://com.android.contacts/data/phones")
//查询联系人
val cursor = resolver.query(uri, null, null, null, null)
while (cursor!!.moveToNext()) {
val name =
cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME))
val number =
cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
Log.d(TAG, "联系人:${name}")
Log.d(TAG, "电话:${number}")
}
cursor.close()
}
The output structure is as follows:
//2023-08-06 17:29:50.535 31719-31719 MainActivity2 com.example.myapplication D Contact: Zhang San //2023-08-06 17:29:50.535 31719-31719 MainActivity2 com.example.myapplication D Tel: 1 (883) 22 //2023-08-06 17:29:50.535 31719-31719 MainActivity2 com.example.myapplication D Contact: si li //2023-08-06 17:29:50.535 31719-31719 MainActivity2 com.example.myapplication D Tel: 10020 //2023-08-06 17:29:50.535 31719-31719 MainActivity2 com.example.myapplication D Contact: bo xiao //2023-08-06 17:29:50.535 31719-31719 MainActivity2 com.example.myapplication D Tel: 10086
Add contacts
Similarly, use contentResolver. Requires permission to write contacts
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
The specific code is as follows:
private fun insertContact() {
val resolver = contentResolver
val values = ContentValues()
//向raw_contacts表插入空数据,拿到uri
val uri = resolver.insert(ContactsContract.RawContacts.CONTENT_URI,values)
//拿到表id
val id = uri?.let { ContentUris.parseId(it) }
//插入姓名
values.clear()
//put id
values.put(ContactsContract.Data.RAW_CONTACT_ID,id)
//put type
values.put(ContactsContract.Data.MIMETYPE,StructuredName.CONTENT_ITEM_TYPE)
//put 值
values.put(StructuredName.GIVEN_NAME,"xiaohong")
resolver.insert(ContactsContract.Data.CONTENT_URI,values)
//插入手机号
values.clear()
//put id
values.put(ContactsContract.Data.RAW_CONTACT_ID,id)
//put type
values.put(ContactsContract.Data.MIMETYPE,Phone.CONTENT_ITEM_TYPE)
//put 值
values.put(Phone.NUMBER,"15541383234")
values.put(Phone.TYPE,Phone.TYPE_MOBILE)
resolver.insert(ContactsContract.Data.CONTENT_URI,values)
//插入邮箱
values.clear()
//put id
values.put(ContactsContract.Data.RAW_CONTACT_ID,id)
//put type
values.put(ContactsContract.Data.MIMETYPE,Email.CONTENT_ITEM_TYPE)
//put 值
values.put(Email.DATA,"[email protected]")
values.put(Email.TYPE,Email.TYPE_WORK)
resolver.insert(ContactsContract.Data.CONTENT_URI,values)
}
The result of the operation is as follows:
update contact
Here we first obtain the contact table id based on the mobile phone number, and this is the only way to obtain the id
@SuppressLint("Range")
private fun getContactByPhone(phone: Long): String? {
val resolver = contentResolver
val uri = Uri.parse("content://com.android.contacts/data/phones/filter/$phone")
val cursor= resolver.query(uri, arrayOf(ContactsContract.Data.CONTACT_ID),null,null,null)
return if (cursor!!.moveToNext()){
val id = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.CONTACT_ID))
cursor.close()
id
}else{
null
}
}
After getting the ContactId, update the data
//根据手机号更新
@SuppressLint("SuspiciousIndentation")
private fun updateContact() {
val id = getContactByPhone(15541383234)
if(id!=null){
val values = ContentValues()
val resolver = contentResolver
values.put(ContactsContract.Data.MIMETYPE,CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
values.put(CommonDataKinds.StructuredName.GIVEN_NAME,"xiaohong_update")
resolver.update(ContactsContract.Data.CONTENT_URI, values,"${ContactsContract.Data.CONTACT_ID}=?",
arrayOf(id)
)
}
}
delete contact
Deleting a contact is relatively simple, we can delete it by the name of the contact
private fun deteleContact() {
val resolver = contentResolver
//根据联系人删除
val ret = resolver.delete(RawContacts.CONTENT_URI, CommonDataKinds.Phone.DISPLAY_NAME+"=?",
arrayOf("si li")
)
if(ret > 0){
Toast.makeText(this, "删除成功", Toast.LENGTH_LONG).show()
}
}
It can also be deleted according to the mobile phone number of the contact, the specific code is as follows:
private fun deteleContact() {
val resolver = contentResolver
//根据手机号删除
val id = getContactByPhone(10020)
val ret = resolver.delete(RawContacts.CONTENT_URI, CommonDataKinds.Phone.CONTACT_ID+"=?",
arrayOf(id)
)
if(ret > 0){
Toast.makeText(this, "删除成功", Toast.LENGTH_LONG).show()
}
}
read sms
In addition, we can also read SMS information through contentResolver, the uri information content://sms/ of the SMS table
Uses such as quick input of verification codes , quick copy of ios SMS verification codes to the input box.
See below for any implementation
First, to read SMS information, permissions are required:
<uses-permission android:name="android.permission.READ_SMS">
</uses-permission>
The specific code is as follows;
private fun getMsgs() {
val uri = Uri.parse("content://sms/")
val resolver = contentResolver
val cursor =resolver.query(uri, arrayOf("address","date","type","body"),null,
null,null)
while (cursor!!.moveToNext()){
val address = cursor.getString(0)
val date = cursor.getString(1)
val type = cursor.getString(2)
val body = cursor.getString(3)
Log.d(TAG,"address:${address},date:${date},type:${type},body:${body}")
}
cursor.close()
}
Here is the uri information for some tables: