目录
读取联系人
添加联系人
更新联系人
删除联系人
读取短信
读取联系人
安卓可以通过contentResolver来读取联系人表,联系人表的Uri信息是:content://com.android.contacts/data/phones 从而输出联系人信息,
需要相关权限:
<uses-permission android:name="android.permission.READ_CONTACTS">
</uses-permission>
具体代码如下:
//需要读联系人权限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()
}
输出结构如下:
//2023-08-06 17:29:50.535 31719-31719 MainActivity2 com.example.myapplication D 联系人:张三 //2023-08-06 17:29:50.535 31719-31719 MainActivity2 com.example.myapplication D 电话:1 (883) 22 //2023-08-06 17:29:50.535 31719-31719 MainActivity2 com.example.myapplication D 联系人:si li //2023-08-06 17:29:50.535 31719-31719 MainActivity2 com.example.myapplication D 电话:10020 //2023-08-06 17:29:50.535 31719-31719 MainActivity2 com.example.myapplication D 联系人:bo xiao //2023-08-06 17:29:50.535 31719-31719 MainActivity2 com.example.myapplication D 电话:10086
添加联系人
同理,使用contentResolver。需要写联系人权限
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
具体代码如下:
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,"163@qq.com")
values.put(Email.TYPE,Email.TYPE_WORK)
resolver.insert(ContactsContract.Data.CONTENT_URI,values)
}
运行结果如下:
更新联系人
这里我们是根据手机号码先获取联系人表id,也仅有这种方式,获取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
}
}
拿到ContactId后,去更新数据
//根据手机号更新
@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)
)
}
}
删除联系人
删除联系人,比较简单,我们可以通过联系人名字,删除
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()
}
}
也可以根据联系人手机号删除,具体代码如下:
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()
}
}
读取短信
除此,我们还可以通过contentResolver读取短信信息,短信表的uri信息content://sms/
用途比如验证码快捷输入,像ios短信验证码快速copy到输入框。
下面看看任何实现
第一,读取短信信息,需要权限:
<uses-permission android:name="android.permission.READ_SMS">
</uses-permission>
具体代码如下;
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()
}
下面是一些表的uri信息: