Android examples for Android OS:Notification
is Enabled Notification
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();/* w w w . j a va 2 s .c o m*/ public static boolean isEnabled(Context context) { List<ComponentName> enabledListeners = getEnabledNotificationListeners(context); if (enabledListeners == null) { return false; } String packageName = context.getPackageName(); for (ComponentName componentName : enabledListeners) { if (packageName.equals(componentName.getPackageName())) { return true; } } return false; } 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; } }