如何在Android中关闭通知的振动 [英] How to turn off vibration of Notifications in android

本文介绍了如何在Android中关闭通知的振动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,该应用程序处理通知中的声音和振动。我正在使用 NotificationListenerService 收听通知。我可以使用 AudioManager 打开或关闭通知的声音,如下所示:

I'm developing an app that handles sound and vibration of apps' notifications. I am listening to notifications using NotificationListenerService. I am able to turn on or off notification's sound using AudioManager as follows:

// It turns off notification's sound
myAudioManager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true); 

// It turns on notification's sound
myAudioManager.setStreamMute(AudioManager.STREAM_NOTIFICATION, false); 

我还尝试使用以下代码打开或关闭通知的振动但是在装有Android KitKat 4.4.4的手机上无法正常运行

I have also tried to turn on or off notification's vibration with following code but it didn't work on my phone which has Android KitKat 4.4.4

// to turn off notification's vibration
myAudioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_OFF);

// to turn on notification's vibration
myAudioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_ON);


推荐答案

这就是我所做的。在我的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.

这篇关于如何在Android中关闭通知的振动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆