Chagall / notification-listener-service-example

This example teaches you how to intercept Android notifications using a built-in service called NotificationListenerService
https://developer.android.com/reference/android/service/notification/NotificationListenerService.html
MIT License
291 stars 86 forks source link

About "isNotificationServiceEnabled"... #5

Open AndroidDeveloperLB opened 6 years ago

AndroidDeveloperLB commented 6 years ago

It's much easier to use this code instead:

fun isNotificationServiceEnabled(context: Context): Boolean =NotificationManagerCompat.getEnabledListenerPackages(context).contains(context.packageName)

Much better than this:

https://github.com/Chagall/notification-listener-service-example/blob/master/app/src/main/java/com/github/chagall/notificationlistenerexample/MainActivity.java

private boolean isNotificationServiceEnabled(){
    String pkgName = getPackageName();
    final String flat = Settings.Secure.getString(getContentResolver(),
            ENABLED_NOTIFICATION_LISTENERS);
    if (!TextUtils.isEmpty(flat)) {
        final String[] names = flat.split(":");
        for (int i = 0; i < names.length; i++) {
            final ComponentName cn = ComponentName.unflattenFromString(names[i]);
            if (cn != null) {
                if (TextUtils.equals(pkgName, cn.getPackageName())) {
                    return true;
                }
            }
        }
    }
    return false;
}
Chagall commented 5 years ago

When this code was implemented, Kotlin was not supported by Android Studio. I did not test the snippet you send, but I don't doubt that it really works. I am just not going to change the method in the codebase to avoid mixing Kotlin and java in the same project, so people don't get confused with mixed languages.

AndroidDeveloperLB commented 5 years ago

You can easily convert it to Java. Here:

public static boolean isNotificationServiceEnabled(Context context ){
    return NotificationManagerCompat.getEnabledListenerPackages(context).contains(context.getPackageName());
}
Chagall commented 5 years ago

If you could make the changes and send a pull request I will be glad to test and accept it if everything runs fine.

AndroidDeveloperLB commented 5 years ago

It's an official API , which was available for a long time: https://developer.android.com/reference/android/support/v4/app/NotificationManagerCompat#getenabledlistenerpackages

AndroidDeveloperLB commented 5 years ago

OK here: https://github.com/Chagall/notification-listener-service-example/pull/7

I also updated various other stuff, to match what we use today, and handled some warnings that the IDE and Lint showed. I could even convert to Kotlin, but I didn't want to :)

AndroidDeveloperLB commented 5 years ago

What do you mean " without user interaction" ? And why do you ask this here? It has nothing to do with the topic . Please ask in a normal, new thread.

AndroidDeveloperLB commented 5 years ago

@Chagall Did you check it out?

Chagall commented 3 years ago

Went afk from this account for a long time, will check this as soon as I can. Better late than never.