How to find which apps are allowed under “Priority app notifications” within Do Not Disturb setting?

三世轮回 提交于 2021-02-07 11:59:19

问题


I am trying to programmatically find out for which apps the Do Not Disturb setting is bypassed exceptionally.

So far, I am using the following code to check whether the phone is set in Do not Disturb mode or not :

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;
    }
}

When I click on the Priority app notiications tab, I get a list of all installed apps for which I get to choose which apps to allow as priority exceptions.

My question is how to programmatically get the list of apps which are allowed as priority exceptions for Do Not Disturb mode, and thereby define the logic replacing the comment in the above code? Any solutions would be thoroughly appreciated.


回答1:


You're looking for a way to determine which apps on the system have a notification importance of Notification.IMPORTANCE_MAX and, sorry to say but this is not possible by a non-system app. You need access to the INotificationService so you can call getImportance(packageName). See the Notification Manager source but it's guarded by a check that ensures you're a system app or the app whose package you passed so reflection is out...

Google allows an app to obtain its own notification importance through the NotificationManager with getImportance() (see docs) but you can't call it with an arbitrary package.

The other answer here references checking out the source from the system settings app and that's exactly what I did and, after a while of tracing through the code, I discovered how they determine which apps should show up in the "Overrides Do Not Disturb" menu by this code here which led me down the path of discovering how we could determine the IMPORTANCE_*

Sorry man, but the suggestions made by the other answerer are also not going to work because they are either incorrect (packages.xml doesn't have the info) or are going to require root which isn't reliable on all devices.




回答2:


You can try following things(some of this things might need privilege access):

  1. Download the source code for settings app and see how settings apps is getting the list.
  2. Analyse packages.xml.Usually lot of info about the apps is stored there.
  3. Check if the list is stored in settings.db


来源:https://stackoverflow.com/questions/43766027/how-to-find-which-apps-are-allowed-under-priority-app-notifications-within-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!