且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Android中关闭通知的振动

更新时间:2022-01-02 22:59:19

这就是我所做的。在我的NotificationListenerService中,我取消了原始通知,关闭了它的振动,然后再次发送出去。这样的代码应该可以工作:

Here's what I did. In my NotificationListenerService, I cancel the original notification, turn off its vibration, and send it out again. Code like this should work:

Integer key = 1;

@Override
public void onNotificationPosted(StatusBarNotification sbn) {
    Notification n = sbn.getNotification();
    if (doINeedToDisableVibration(sbn)) {
        cancelNotification(sbn.getKey());
        n.defaults &= ~Notification.DEFAULT_VIBRATE;
        n.vibrate = null;
        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            synchronized(key) {
                nm.notify(key++, n);
            }
    }
}

在doINeedToDisableVibration(sbn)中,请确保在通知中启用了振动功能,或者检查sbn.packageName与您自己的包名称是否不同,否则可能会产生无限循环。

In doINeedToDisableVibration(sbn), either make sure that vibration is on in the notification, or else check that sbn.packageName is NOT the same as your own package name, or you have a danger of generating an endless loop.

我不确定这是否适用于所有通知。但这确实适用于我的HTC One A9 6.0版的MMS通知。

I am not sure this will work with all notifications. But it did work with MMS notifications on my HTC One A9 with 6.0.