[英]How to turn off vibration of Notifications in android
我正在开发一个应用程序来处理应用程序通知的 声音和振动 。 我正在使用NotificationListenerService
收听NotificationListenerService
。 我能够使用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);
我也尝试使用以下代码打开或关闭通知的振动 ,但它在我的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中,我取消原始通知,关闭其振动,然后再将其发送出去。 像这样的代码应该工作:
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与您自己的包名称是否相同,否则您可能会产生无限循环。
我不确定这适用于所有通知。 但它确实适用于我的HTC One A9 6.0上的彩信通知。
在API 16中不推荐使用setVibrateSetting api。根据android开发者网站,
应用程序应根据当前振铃模式维护自己的振动策略,可通过getRingerMode()查询。
因此,您尝试使用振铃模式启用/禁用振动。
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
用于设置静音模式:
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
对于正常模式:
audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
对于环振动模式:
audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.