如何在"请勿打扰"设置中的"优先级应用程序通知"下查找允许的应用程序?

Sou*_*der 9 android android-notifications

我正在尝试以编程方式找出特殊情况下绕过"请勿打扰"设置的应用程序.

在此输入图像描述

到目前为止,我使用以下代码检查手机是否设置为"请勿打扰"模式:

public static boolean isDnDModeEnabled(Context context)
{
    if(Build.VERSION.SDK_INT <23)
        return false;

    try {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        int filterValue = notificationManager.getCurrentInterruptionFilter();
        switch(filterValue)
        {
            case NotificationManager.INTERRUPTION_FILTER_ALL : Log.d("DND","Interruption filter all");
                break;
            case NotificationManager.INTERRUPTION_FILTER_ALARMS : Log.d("DND","Interruption filter alarms");
                break;
            case NotificationManager.INTERRUPTION_FILTER_PRIORITY : Log.d("DND","Interruption filter priority");
                break;
            case NotificationManager.INTERRUPTION_FILTER_UNKNOWN : Log.d("DND","Interruption filter unknown");
                break;
            case NotificationManager.INTERRUPTION_FILTER_NONE : Log.d("DND","Interruption filter none");
                break;

        }
        if(filterValue == NotificationManager.INTERRUPTION_FILTER_ALL)
            return false;
        else if(filterValue == NotificationManager.INTERRUPTION_FILTER_PRIORITY)
        {
            //Logic based on which apps are allowed as priority

            return true; //or false
        }
        else
            return true;
    }
    catch(Exception e)
    {
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

当我点击优先级应用程序通知选项卡时,我会获得所有已安装应用程序的列表,我可以从中选择允许哪些应用程序作为优先级例外.

我的问题是如何以编程方式获取允许作为"请勿打扰"模式的优先级例外的应用程序列表,从而定义替换上述代码中的注释的逻辑?任何解决方案都将得到充分肯

Hyp*_*cle 5

您正在寻找一种方法来确定系统上的哪些应用程序具有通知重要性Notification.IMPORTANCE_MAX,很抱歉,但这对于非系统应用程序来说是不可能的。您需要访问INotificationService以便您可以致电getImportance(packageName)。请参阅通知管理器源代码,但它受到检查的保护,以确保您是系统应用程序或您传递的包的应用程序,因此反射已消失......

NotificationManagerGoogle 允许应用程序通过with getImportance()请参阅文档)获取自己的通知重要性,但您不能使用任意包调用它。

这里的另一个答案引用了从系统设置应用程序检查源代码,这正是我所做的,经过一段时间的代码跟踪,我发现他们如何确定哪些应用程序应该显示在“覆盖请勿打扰”菜单中通过这里的代码,它引导我发现我们如何确定IMPORTANCE_*

抱歉,其他回答者提出的建议也不起作用,因为它们要么不正确(packages.xml 没有信息),要么需要 root,这在所有设备上都不可靠。