get Enabled Notification Listeners - Android Android OS

Android examples for Android OS:Notification

Description

get Enabled Notification Listeners

Demo Code


import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class Main{
    private static final String TAG = NotificationListenerServiceUtil.class
            .getSimpleName();//from  w w  w  .j a v a 2s . c  om
    
    private static List<ComponentName> getEnabledNotificationListeners(
            Context context) {
        String permissionName;

        try {
            Field field = Settings.Secure.class
                    .getField("ENABLED_NOTIFICATION_LISTENERS");
            permissionName = (String) field.get(null);
        } catch (NoSuchFieldException e) {
            return null;
        } catch (IllegalAccessException e) {
            return null;
        }

        ContentResolver contentResolver = context.getContentResolver();
        String rawListeners = Settings.Secure.getString(contentResolver,
                permissionName);
        if (rawListeners == null || "".equals(rawListeners)) {
            return null;
        }

        String[] listeners = rawListeners.split(":");

        ArrayList<ComponentName> componentNames = new ArrayList<ComponentName>();
        for (String listener : listeners) {
            LogUtil.d(TAG, "enabled listener : " + listener);
            ComponentName cn = ComponentName.unflattenFromString(listener);
            if (cn != null) {
                componentNames.add(cn);
            }
        }
        return componentNames;
    }
}

Related Tutorials