Android NotificationListenerService#onNotificationRemoved(..., int reason)未被系统调用。
创始人
2024-09-30 12:30:44
0

解决这个问题的方法是确保正确地实现NotificationListenerService类,并正确注册和启动服务。以下是一个包含代码示例的解决方法:

  1. 确保在AndroidManifest.xml文件中正确注册NotificationListenerService类。在标签内添加以下代码:

    
        
    

  1. 创建一个继承自NotificationListenerService的自定义服务类(例如MyNotificationListenerService)。在该类中实现onNotificationRemoved()方法。确保在方法中添加日志输出,以便在调试时查看是否被调用。
public class MyNotificationListenerService extends NotificationListenerService {

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap, int reason) {
        super.onNotificationRemoved(sbn, rankingMap, reason);
        Log.d("NotificationListener", "onNotificationRemoved: " + reason);
    }

    // 其他需要实现的方法...
}
  1. 启动服务。可以在MainActivity或其他适当的位置启动服务。确保在启动服务之前已经获得了BIND_NOTIFICATION_LISTENER_SERVICE权限。
public class MainActivity extends AppCompatActivity {

    private static final int REQUEST_NOTIFICATION_ACCESS = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 检查是否具有通知访问权限
        if (!isNotificationAccessGranted()) {
            // 启动系统设置页面以授予通知访问权限
            startActivityForResult(new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS), REQUEST_NOTIFICATION_ACCESS);
        } else {
            // 已经具有通知访问权限,启动服务
            startNotificationListenerService();
        }
    }

    // 检查是否具有通知访问权限
    private boolean isNotificationAccessGranted() {
        String packageName = getPackageName();
        String flat = Settings.Secure.getString(getContentResolver(), "enabled_notification_listeners");
        if (flat != null) {
            return flat.contains(packageName);
        }
        return false;
    }

    // 启动服务
    private void startNotificationListenerService() {
        Intent intent = new Intent(this, MyNotificationListenerService.class);
        startService(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_NOTIFICATION_ACCESS) {
            // 用户返回后,再次检查通知访问权限
            if (isNotificationAccessGranted()) {
                // 启动服务
                startNotificationListenerService();
            } else {
                // 没有授予通知访问权限,可以做相应的处理
            }
        }
    }
}

通过以上步骤,可以确保正确地实现和启动NotificationListenerService服务,并在onNotificationRemoved()方法中正确处理通知移除事件。

相关内容

热门资讯

有挂透视!wepoker钻石怎... 有挂透视!wepoker钻石怎么看底牌,微乐家乡自建房辅助app,妙计教程(今日头条)-哔哩哔哩1、...
解谜透视!wepoker私人局... 解谜透视!wepoker私人局辅助器怎么用,微乐陕西麻将破解,法门教程(确实有挂)-哔哩哔哩1、解谜...
分享透视!hhpoker德州牛... 分享透视!hhpoker德州牛仔视频,微信小程序微乐辅助器免费下载,妙计教程(有挂细节)-哔哩哔哩小...
解密透视!竞技联盟辅助插件,微... 解密透视!竞技联盟辅助插件,微乐安徽小程序辅助,机巧教程(有挂解密)-哔哩哔哩1、下载好竞技联盟辅助...
专业透视!德普之星透视免费,微... 专业透视!德普之星透视免费,微乐自建房辅助软件有哪些功能,指南教程(有挂分析)-哔哩哔哩1、在德普之...
专业透视!wpk有作弊吗,微乐... 专业透视!wpk有作弊吗,微乐小程序辅助脚本平台,手册教程(有挂存在)-哔哩哔哩1、很好的工具软件,...
开挂透视!wepoker透视脚... 开挂透视!wepoker透视脚本安卓,微乐手游辅助脚本平台,秘籍教程(有挂方略)-哔哩哔哩1、让任何...
详情透视!wepoker有没有... 详情透视!wepoker有没有插件,微乐江西小程序辅助,烘培教程(有挂方式)-哔哩哔哩1、wepok...
推荐透视!wepoker分析,... 推荐透视!wepoker分析,小程序微乐辅助器2.0,项目教程(有挂头条)-哔哩哔哩1、许多玩家不知...
曝光透视!德普之星透视软件免费... 曝光透视!德普之星透视软件免费入口官网,微乐自建房安装挂,法门教程(真实有挂)-哔哩哔哩德普之星透视...