微信消息语音播报秒实现

1. 监听系统消息通知

注册一个监听系统消息的服务

复制代码
<service
            android:name=".MyNotificationListenerService"
            android:exported="true"
            android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" >
            <intent-filter>
                <action android:name="android.service.notification.NotificationListenerService" />
            </intent-filter>
        </service>

实现这个服务,解析消息通知,如果是来自微信,则组装成微信消息,提取发送者,和消息内容,更新到livedata中

Kotlin 复制代码
class MyNotificationListenerService : NotificationListenerService() {

    companion object{
        val wxMessage = MutableLiveData<WxMessage>()
    }
    override fun onNotificationPosted(sbn: StatusBarNotification) {
        // 捕获通知
        val notification = sbn.notification
        // 根据用户设置过滤通知
        println("linlian ${sbn.packageName},${sbn.notification.tickerText}")

        if (PACKAGE_WX == sbn.packageName) {
            convertToWxMessage(sbn)?.let {
                wxMessage.postValue(it)
            }

        }
    }

    /**
     * 将微信消息转换成发送者,和消息内容
     */
    private fun convertToWxMessage(sbn: StatusBarNotification): WxMessage? {
        try {
            sbn.notification.tickerText?.let {
                val splits = it.split(
                    delimiters = arrayOf(":"),
                    ignoreCase = false,
                    limit = 2
                )
                if (splits.size == 2) {
                   return  WxMessage(splits[0], splits[1])
                }

            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return null

    }
}

怎么启动这个消息监听服务呢

Kotlin 复制代码
  /*
    显示设置界面,运行我们的应用监听服务
     */
    private fun startNotificationSetting() {
        startActivity(Intent(ACTION_NOTIFICATION_LISTENER_SETTINGS));
    }

这时候会启动系统设置界面,我们需要允许我们的应用

如何判断系统是否允许我们监听了呢

Kotlin 复制代码
 /**
     * 判断应用是否有权限
     */
    private fun isServiceSettingEnable(): Boolean {
        var enable = false
        val packageName = packageName
        val flat: String =
            Settings.Secure.getString(contentResolver, "enabled_notification_listeners")
        if (flat != null) {
            enable = flat.contains(packageName)
        }
        return enable
    }

允许了则直接跳过,不允许的话,启动设置界面,引导用户设置。

2.语音播报

语音播报的话,主要使用TextToSpeech

初始化,设置中文

Kotlin 复制代码
 tts = TextToSpeech(this) { status ->
            println("linlian status=$status")
            if (TextToSpeech.SUCCESS == status) {
                tts.setLanguage(Locale.CHINESE)
            }
        }

监听livedata

Kotlin 复制代码
wxMessage.observe(this, object : Observer<WxMessage> {
            override fun onChanged(value: WxMessage) {
                tts.speak(
                    "收到来自${value.sender}的消息,${value.message}",
                    TextToSpeech.QUEUE_FLUSH,
                    null
                )
            }

        })

that's it

相关推荐
飞哥数智坊19 小时前
GPT-5-Codex 发布,Codex 正在取代 Claude
人工智能·ai编程
倔强青铜三19 小时前
苦练Python第46天:文件写入与上下文管理器
人工智能·python·面试
虫无涯20 小时前
Dify Agent + AntV 实战:从 0 到 1 打造数据可视化解决方案
人工智能
Dm_dotnet1 天前
公益站Agent Router注册送200刀额度竟然是真的
人工智能
算家计算1 天前
7B参数拿下30个世界第一!Hunyuan-MT-7B本地部署教程:腾讯混元开源业界首个翻译集成模型
人工智能·开源
机器之心1 天前
LLM开源2.0大洗牌:60个出局,39个上桌,AI Coding疯魔,TensorFlow已死
人工智能·openai
Juchecar1 天前
交叉熵:深度学习中最常用的损失函数
人工智能
林木森ai1 天前
爆款AI动物运动会视频,用Coze(扣子)一键搞定全流程(附保姆级拆解)
人工智能·aigc
聚客AI1 天前
🙋‍♀️Transformer训练与推理全流程:从输入处理到输出生成
人工智能·算法·llm
BeerBear1 天前
【保姆级教程-从0开始开发MCP服务器】一、MCP学习压根没有你想象得那么难!.md
人工智能·mcp