Я использовал ниже для запуска NotificationListenerService, служба запускается автоматически, но проблема возникает, когда приложение убито, в этом случае я прошу пользователя переключить уведомителя в настройках, что делает Перезапустить службу прослушивателяопять же, но теперь есть 2 экземпляра, которые прослушивают отправленные уведомления, и снова, сколько раз пользователь переключает прослушиватель уведомлений для моего приложения в настройках, создается много экземпляров или клиентов, которые теперь привязаны к прослушивателю уведомленийservice.
код является базовым и доступен в большинстве примеров. Мой вопрос заключается в том, чтобы предотвратить создание нескольких экземпляров / клиентов, которые создаются, когда пользователь переключает настройки NotificationListener, благодаря вам за поддержку.
public class MyListener extends NotificationListenerService {
private static final int EVENT_UPDATE_CURRENT_NOS = 0;
public static List<StatusBarNotification[]> mCurrentNotifications = new
ArrayList<StatusBarNotification[]>();
public static int mActiveNotificationsCount = 0;
public static StatusBarNotification mPostedNotification;
public static StatusBarNotification mRemovedNotification;
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
return super.onBind(intent);
}
@Override
public boolean onUnbind(Intent mIntent) {
return super.onUnbind(mIntent);
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
mPostedNotification = sbn;
// Get the text from notification
// CharSequence notificationText = sbn.getNotification().extras.getCharSequence(Notification.EXTRA_TEXT);
// Show a toast with your Notification Text
// Toast.makeText(getApplicationContext(), notificationText, Toast.LENGTH_SHORT).show();
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
}
public void onListenerConnected() {
// Add some prints here to check if our service is connected to OS or not?
}
public static StatusBarNotification[] getCurrentNotifications() {
if (mCurrentNotifications.size() == 0) {
return null;
}
return mCurrentNotifications.get(0);
}
}
```````````````````````````````````