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); 
      } 
     } 

    } 
} 

如果上面有什麼不明白的話請原諒我。任何一段代碼和後面都會對我非常有幫助。提前致謝。

回答

相關問題