0

У меня есть требование получить все уведомления о приложениях, которые уже существуют в панели уведомлений. Для этой цели я использую NotificationListenerService вместе с BroadcastReceiver для получения определенных параметров, таких как Notification.EXTRA_TITLE, Notification.EXTRA_SUB_TEXT и т. Д. Но проблема, с которой сталкивается NotificationListenerService, заключается в том, что я не смог получить существующее уведомление на панели, где я мог получить будущие уведомления, которые я получаю, когда приложение находится на переднем плане. Пожалуйста, помогите мне в этом, есть ли какое-либо свойство, чтобы получить все уведомления о приложении, которые уже представлены в панели уведомлений. Я также отправляю код, который я использую для вашей справки.Извлечение всех приложений из панели уведомлений android

SimpleKitkatNotificationListener.java

public class SimpleKitkatNotificationListener extends NotificationListenerService { 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     //android.os.Debug.waitForDebugger(); 
    } 

    @Override 
    public void onNotificationPosted(StatusBarNotification sbn) { 

     Notification mNotification=sbn.getNotification(); 

     Log.i("notification",mNotification.tickerText.toString()); 
     if (mNotification!=null){ 
      Bundle extras = mNotification.extras; 

      Intent intent = new Intent(MainActivity.INTENT_ACTION_NOTIFICATION); 
      intent.putExtras(mNotification.extras); 
      sendBroadcast(intent); 

      Notification.Action[] mActions=mNotification.actions; 
      if (mActions!=null){ 
       for (Notification.Action mAction:mActions){ 
        int icon=mAction.icon; 
        CharSequence actionTitle=mAction.title; 
        PendingIntent pendingIntent=mAction.actionIntent; 
       } 
      } 
     } 
    } 

    @Override 
    public void onNotificationRemoved(StatusBarNotification sbn) { 

    } 
} 

А в классе я активность получать обратные вызовы через BroadcastReceiver следующим

MainActivity.java

общественный класс MainActivity расширяет активность {

protected MyReceiver mReceiver = new MyReceiver(); 
public static String INTENT_ACTION_NOTIFICATION = "it.gmariotti.notification"; 

protected TextView title; 
protected TextView text; 
protected TextView subtext; 
protected ImageView largeIcon; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    //Retrieve ui elements 
    title = (TextView) findViewById(R.id.nt_title); 
    text = (TextView) findViewById(R.id.nt_text); 
    subtext = (TextView) findViewById(R.id.nt_subtext); 
    largeIcon = (ImageView) findViewById(R.id.nt_largeicon); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.main, menu); 
    return super.onCreateOptionsMenu(menu); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.action_autorize: 
      Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"); 
      startActivity(intent); 
      return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    if (mReceiver == null) mReceiver = new MyReceiver(); 
    registerReceiver(mReceiver, new IntentFilter(INTENT_ACTION_NOTIFICATION)); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    unregisterReceiver(mReceiver); 
} 

public class MyReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     if (intent != null) { 
      Bundle extras = intent.getExtras(); 
      String notificationTitle = extras.getString(Notification.EXTRA_TITLE); 
      int notificationIcon = extras.getInt(Notification.EXTRA_SMALL_ICON); 
      Bitmap notificationLargeIcon = ((Bitmap) extras.getParcelable(Notification.EXTRA_LARGE_ICON)); 
      Bitmap notificationSmallIcon = ((Bitmap) extras.getParcelable(Notification.EXTRA_SMALL_ICON)); 

      CharSequence notificationText = extras.getCharSequence(Notification.EXTRA_TEXT); 
      CharSequence notificationSubText = extras.getCharSequence(Notification.EXTRA_SUB_TEXT); 

      title.setText(notificationTitle); 
      text.setText(notificationText); 
      subtext.setText(notificationSubText); 

      if (notificationLargeIcon != null) { 
       largeIcon.setImageBitmap(notificationLargeIcon); 
      }else if(notificationSmallIcon !=null){ 
       largeIcon.setImageBitmap(notificationSmallIcon); 
      } 
     } 

    } 
} 

Если что-либо выше не ясно, простите меня. Любая часть кода и задняя часть будут очень полезны для меня. Заранее спасибо.

ответ

1

Попробуйте прочитать документацию:

http://developer.android.com/reference/android/service/notification/NotificationListenerService.html

Существует пример есть, как вы определяете услугу для обработки связывания с NLS. Это позволит вам получить экземпляр NLS и использовать

http://developer.android.com/reference/android/service/notification/NotificationListenerService.html#getActiveNotifications()

Смежные вопросы