Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

About "isNotificationServiceEnabled"... #5

Open
AndroidDeveloperLB opened this issue Aug 9, 2018 · 8 comments
Open

About "isNotificationServiceEnabled"... #5

AndroidDeveloperLB opened this issue Aug 9, 2018 · 8 comments

Comments

@AndroidDeveloperLB
Copy link

AndroidDeveloperLB commented Aug 9, 2018

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
Copy link
Owner

Chagall commented Jun 4, 2019

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.

@Chagall Chagall closed this as completed Jun 4, 2019
@AndroidDeveloperLB
Copy link
Author

AndroidDeveloperLB commented Jun 4, 2019

You can easily convert it to Java. Here:

public static boolean isNotificationServiceEnabled(Context context ){
    return NotificationManagerCompat.getEnabledListenerPackages(context).contains(context.getPackageName());
}

@Chagall Chagall reopened this Jun 5, 2019
@Chagall
Copy link
Owner

Chagall commented Jun 5, 2019

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
Copy link
Author

@AndroidDeveloperLB
Copy link
Author

OK here:
#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
Copy link
Author

AndroidDeveloperLB commented Jul 1, 2019

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
Copy link
Author

@Chagall Did you check it out?

@Chagall
Copy link
Owner

Chagall commented Mar 19, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants