美文网首页程序员Android开发Android开发经验谈
Android关于联系人、短信的各种操作总结

Android关于联系人、短信的各种操作总结

作者: 奔跑吧李博 | 来源:发表于2022-12-15 00:01 被阅读0次
    跳转拨号界面
    var intent = Intent(Intent.ACTION_DIAL)
                var data = Uri.parse("tel:" + "10086")
                intent.data = data
                startActivity(intent)
    
    直接拨打电话
    var intent = Intent(Intent.ACTION_CALL)
                var data = Uri.parse("tel:" + "10086")
                intent.data = data
                startActivity(intent)
    
    跳转发送短信界面
                val uri = Uri.parse("smsto:10086")
                startActivity(Intent(Intent.ACTION_SENDTO, uri).apply {
                    putExtra("sms_body", "")
                })
    
    搜索短信内容
        /**
         * 模糊查询短信
         */
        @SuppressLint("Range")
        fun getSmsFuzzyQueryByKey(context: Context, key: String): List<ContactBean>? {
            var list = ArrayList<ContactBean>()
            //需要查询的字段
            var projection = arrayOf(Telephony.Sms.ADDRESS, Telephony.Sms.BODY)
            val selection = java.lang.StringBuilder()
            selection.append(Telephony.Sms.BODY)
            if (key.contains("\'")) {
                selection.append(" LIKE '%${key.replace("\'", "")}%' ")
            } else {
                selection.append(" LIKE '%$key%' ")
            }
            //Telephony.Sms.CONTENT_URI 短信表
            var cursor = context.contentResolver.query(Telephony.Sms.CONTENT_URI, projection, selection.toString(), null, null)
            cursor?.let {
                while (it.moveToNext()) {
                    var number = it.getString(it.getColumnIndex(Telephony.Sms.ADDRESS))
                    var content = it.getString(it.getColumnIndex(Telephony.Sms.BODY))
                    list.add(ContactBean(number, content))
                }
                it.close()
            }
            return list
        }
    
    搜索联系人
        /**
         * 模糊查询联系人
         */
        @SuppressLint("Range")
        fun getContactsFuzzyQueryByKey(context: Context, key: String): List<ContactBean> {
            var list = ArrayList<ContactBean>()
            //ContactsContract.CommonDataKinds.Phone 联系人表
            val projection = arrayOf(ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER)
            val selection = StringBuilder()
            selection.append(ContactsContract.Contacts.DISPLAY_NAME)
            if (key.contains("\'")) {
                selection.append(" LIKE '%${key.replace("\'", "")}%' ")
            } else {
                selection.append(" LIKE '%$key%' ")
            }
            var cursor: Cursor? = context.contentResolver.query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                projection, selection.toString(), null, null)
            cursor?.let {
                while (it.moveToNext()) {
                    var name = it.getString(it.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME))
                    var number = cursor.getString(it.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
                    list.add(ContactBean(number, name))
                }
                it.close()
            }
            return list
        }
    

    相关文章

      网友评论

        本文标题:Android关于联系人、短信的各种操作总结

        本文链接:https://www.haomeiwen.com/subject/yroemrtx.html