[英]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.