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()方法中正确处理通知移除事件。

相关内容

热门资讯

反观!pokemomo辅助软件... 反观!pokemomo辅助软件,wpk俱乐部是做什么的,一贯是真的有挂(有挂分析)一、wpk俱乐部是...
必备科技!pokermaste... 必备科技!pokermaster破解版(辅助挂)切实真的是有挂(有挂方针辅助方法)1、上手简单,内置...
专业讨论!HH平台挂(辅助挂)... 专业讨论!HH平台挂(辅助挂)总是存在有挂(存在有挂辅助软件)1、下载好脚本下载之后点击打开,先需要...
教程辅助!wepoker辅助透... 教程辅助!wepoker辅助透视软件(辅助挂)果然是有挂(存在有挂辅助插件)1、免费辅助多个强度级别...
一分钟了解!wepoker免费... 一分钟了解!wepoker免费辅助器(辅助挂)果然存在有挂(竟然有挂辅助技巧)1、该软件可以轻松地帮...
总算了解!wepoker底牌透... 总算了解!wepoker底牌透视脚本(辅助挂)竟然是有挂(确实有挂辅助攻略)1、辅助软件下载优化,底...
让我来分享经验!wpk辅助哪里... 让我来分享经验!wpk辅助哪里买(辅助挂)一贯存在有挂(证实有挂辅助技巧)1、模拟器是什么优化,俱乐...
总算明白!wejoker开挂(... 总算明白!wejoker开挂(辅助挂)真是是有挂(有挂详细辅助教程)1)有没有挂:进一步探索有辅助吗...
信息共享!pokemmo手机版... 信息共享!pokemmo手机版修改器(辅助挂)竟然存在有挂(有挂教程辅助脚本)1、下载好透视辅助下载...
玩家必备科技!wepoker有... 玩家必备科技!wepoker有辅助插件吗(辅助挂)都是存在有挂(竟然有挂辅助软件)1、免费脚本咨询教...