Android:某些通知可以取消,有些则不会

时间:2015-05-19 02:31:46

标签: android android-notifications

我写了一个Android应用来取消所有通知。

public class NotificationListener extends NotificationListenerService {

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        Log.d("NotificationListener", "package:" + sbn.getPackageName());
        Log.d("NotificationListener", "tag:" + sbn.getTag());
        Log.d("NotificationListener", "id:" + sbn.getId());
        cancelAllNotifications();
    }


    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        Log.d("NotificationListener", "remove package:" + sbn.getPackageName());
        Log.d("NotificationListener", "remove tag:" + sbn.getTag());
        Log.d("NotificationListener", "remove id:" + sbn.getId());
    }
}
AndroidManifest.xml

中的

<service android:name=".NotificationListener"
        android:label="@string/service_name"
        android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
        <intent-filter>
            <action android:name="android.service.notification.NotificationListenerService" />
        </intent-filter>
</service>

我可以取消以下套餐通知:

05-19 10:25:22.422  1289  1307 D NotificationListener : remove package:com.android.providers.downloads
05-19 10:25:22.422  1289  1307 D NotificationListener : remove tag:1:com.android.vending
05-19 10:25:22.422  1289  1307 D NotificationListener : remove id:0

但我无法取消以下套餐通知:

05-19 10:25:20.492  1289  1306 D NotificationListener : package:com.android.settings
05-19 10:25:20.492  1289  1306 D NotificationListener : tag:null
05-19 10:25:20.492  1289  1306 D NotificationListener : id:2130837807

任何人都知道为什么?

提前致谢。

埃里克

1 个答案:

答案 0 :(得分:1)

某些通知无法取消,因为它们已设置为FLAG_NO_CLEAR。查看文档here