Android: 有的通知可以取消,有的不可以

Android: Some notification can be cancelled and some not

我写了一个 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

有人知道为什么吗?

提前致谢。

埃里克

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