Last active
March 2, 2018 17:05
-
-
Save gbzarelli/b9fdc2d66c0b1115ed3221f090666ebc to your computer and use it in GitHub Desktop.
Verifica se o serviço de notificação esta habilitado para o aplicativo.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// CRIAR SERVIÇO PARA INTERCEPTAR NOTIFICAÇES: | |
class NotificationService : android.service.notification.NotificationListenerService() { | |
override fun onCreate() { | |
super.onCreate() | |
} | |
override fun onNotificationPosted(sbn: StatusBarNotification) { | |
} | |
override fun onNotificationRemoved(sbn: StatusBarNotification) { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- DECLARAR NO MANIFEST --> | |
<service | |
android:name=".services.NotificationService" | |
android:label="@string/notification_label" | |
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"> | |
<intent-filter> | |
<action android:name="android.service.notification.NotificationListenerService" /> | |
</intent-filter> | |
</service> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Is Notification Service Enabled. | |
* Verifies if the notification listener service is enabled. | |
* Got it from: https://github.com/kpbird/NotificationListenerService-Example/blob/master/NLSExample/src/main/java/com/kpbird/nlsexample/NLService.java | |
* | |
* @return True if eanbled, false otherwise. | |
*/ | |
private boolean isNotificationServiceEnabled(Context context) { | |
String pkgName = context.getPackageName(); | |
final String flat = Settings.Secure.getString(context.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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment