【问题标题】:How to turn off the default vibration of a notification如何关闭通知的默认振动
【发布时间】:2012-03-14 03:08:43
【问题描述】:
我在 Android 4.0 上使用 Galaxy Nexus,我在“设置”中将静音模式设置为振动。我使用 NotificationManager.notify 发送通知。我没有设置 Notification.vibrate,我什至使用 myNotification.defaults &= ~Notification.DEFAULT_VIBRATE 来禁用振动。但是在调用 NotifcationManager.notify 后它仍然会振动。
谁能告诉我如何在振动模式下关闭通知的振动?
【问题讨论】:
标签:
android
notifications
vibration
【解决方案1】:
使用以下代码:
notification.defaults = Notification.DEFAULT_LIGHTS;
//or
notification.defaults = Notification.DEFAULT_SOUND;
【解决方案2】:
动态管理通知设置:
notification.defaults = Notification.DEFAULT_LIGHTS;
if(/*sound enabled*/)
notification.defaults |= Notification.DEFAULT_SOUND;
if(/*vibration enabled*/)
notification.defaults |= Notification.DEFAULT_VIBRATE;
【解决方案3】:
要在收到通知时禁用振动,我使用此代码。
notification.vibrate = new long[] { -1 };
而且它工作得很好。
【解决方案4】:
首先将您的振动设置按钮的值存储在共享偏好中。然后将此代码放在收到通知的位置。
SharedPreferences preferences = context.getSharedPreferences("VIBRATE",
0);
boolean vibrate = preferences.getBoolean("vibrate", true);
if (vibrate) {
notification.defaults |= Notification.DEFAULT_VIBRATE;
}
【解决方案5】:
也可以针对特定包使用下面的 adb 命令来完成
adb shell
cmd appops set package_name VIBRATE ignore
上述命令将禁用 package_name 包的所有振动。