简体   繁体   English

如何在android中关闭Notifications的振动

[英]How to turn off vibration of Notifications in android

I'm developing an app that handles sound and vibration of apps' notifications . 我正在开发一个应用程序来处理应用程序通知的 声音和振动 I am listening to notifications using NotificationListenerService . 我正在使用NotificationListenerService收听NotificationListenerService I am able to turn on or off notification's sound using AudioManager as follows: 我能够使用AudioManager 打开或关闭通知声音,如下所示:

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

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

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 我也尝试使用以下代码打开或关闭通知的振动 ,但它在我的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);

Here's what I did. 这就是我做的。 In my NotificationListenerService, I cancel the original notification, turn off its vibration, and send it out again. 在我的NotificationListenerService中,我取消原始通知,关闭其振动,然后再将其发送出去。 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);
            }
    }
}

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. 在doINeedToDisableVibration(sbn)中,要么确保通知中存在振动,要么检查sbn.packageName与您自己的包名称是否相同,否则您可能会产生无限循环。

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. 但它确实适用于我的HTC One A9 6.0上的彩信通知。

setVibrateSetting api is deprecated in API 16. As per android developer site, 在API 16中不推荐使用setVibrateSetting api。根据android开发者网站,

Applications should maintain their own vibrate policy based on current ringer mode that can be queried via getRingerMode(). 应用程序应根据当前振铃模式维护自己的振动策略,可通过getRingerMode()查询。

So, you try with Ringer mode to enable/disable vibration. 因此,您尝试使用振铃模式启用/禁用振动。

 AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);

for setting silent mode : 用于设置静音模式:

audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);

For normal mode : 对于正常模式:

audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);

For ring vibrate mode: 对于环振动模式:

audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM