android通知收不到原因NotificationListener

本文探讨了Launcher无法接收应用通知的问题,并提供了两种解决方案:一种是在后台通过代码设置通知监听权限,但该方法不稳定;另一种是引导用户手动开启权限,推荐使用此方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近发现launcher收不到应用通知,问题是NotificationListener监听权限关闭了,具体什么原因关闭的现在不知道,网上说应用被kill了再次启动时没有加入该权限,但我验证过kill掉应用不会有这种情况,可能是我在后台通过下面setDefaultNotificationAccess方法重新加入了权限,这些后台加权限的方法不好用,有时成功有时不成功而且不会马上生效,建议主动跳转到设置权限界面。

//主动调到设置界面打开通知监听权限
public void setNotificationAccess(){
    Intent intent = new         
    Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}

                    
//后台写入
private void setDefaultNotificationAccess() {
        //获取所有通知监听
        final String flat = Settings.Secure.getString(getContentResolver(),
                "enabled_notification_listeners");
        if (flat != null && !"".equals(flat)) {
            //判断是否已经包含了你的,包名+通知监听类
            if (flat.contains("xxxx/xxxx.NotificationListener")) {
                Log.d(TAG, "Nothing to do");
            } else {
                Log.d(TAG, "append xxx");
                StringBuilder flatString = new StringBuilder(flat);
                //追加包名+通知监听类flatString.append(":com.xxx.xxx/com.xxx.xxx.xxx.NotificationListener");
                try {//写入
                    Settings.Secure.putString(getContentResolver(),
                            "enabled_notification_listeners", flatString.toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } else {
            try {//写入
                Settings.Secure.putString(getContentResolver(),
                        "enabled_notification_listeners",
                        "com.xxx.xxx/com.xxx.xxx.xxx.NotificationListener");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值