禁用振动的通知 [英] Disable vibration for a notification
问题描述
我使用的通知写一个应用程序。谷歌开发的指导方针鼓励开发者提供程序设置自定义通知(禁用振动,设置通知的声音... ...),所以我试图禁用振动通知,如果用户设置这种方式。
I'm writing an app using notification. Google developer guidelines encourages developers to provide settings to customize the notifications (disable vibration, set notification sound...), so I am trying to disable vibration for notifications if the user set it that way.
我使用 NotificationCompat.Builder
以创建通知,像这样的:
I am using NotificationCompat.Builder
to create the notification, like this:
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(Application.getContext())
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_MAX)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(largeIconBitmap)
.setAutoCancel(true)
.setContentIntent(resultPendingIntent)
.setContentTitle(title)
.setContentText(content);
我尝试不同的方法来禁用通知:
I tried different ways to disable notifications:
notificationBuilder.setVibrate(null);
notificationBuilder.setVibrate(new long[]{0l, 0l});
notificationBuilder.setDefaults(Notification.DEFAULT_ALL | ~Notification.DEFAULT_VIBRATE);
notificationBuilder.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND);`
我也试图建立通知和变化值所产生的对象:
I also tried to build the notification and change values on the resulting object:
Notification notification = notificationBuilder.build();
notification.vibrate = null;
但出现在通知时手机仍然振动。
But the phone still vibrates when the notification appears.
我如何禁用振动通知?
推荐答案
一个长期的试验和功放后,错误会议,我想我终于明白了什么是错的。
After a long trial & error session, I think I finally understood what's wrong.
问题就出在这个指令 notificationBuilder.setDefaults(Notification.DEFAULT_ALL)
。
无论你通过什么样的参数设置后, notificationBuilder.setVibrate()
DEFAULT_ALL
或 DEFAULT_VIBRATE
将被丢弃。有人在谷歌必须决定给予较高的precedence到使用setdefaults
比 setVibrate
。
No matter what parameter you pass to notificationBuilder.setVibrate()
after setting DEFAULT_ALL
or DEFAULT_VIBRATE
will be silently discarded. Someone at Google must have decided to give a higher precedence to setDefaults
than to setVibrate
.
这就是我结束了禁用的振动在我的应用程序的通知:
This is how I ended up disabling vibration for notifications in my app:
notificationBuilder.setDefaults(Notification.DEFAULT_LIGHT | Notification.DEFAULT_SOUND)
.setVibrate(new long[]{0l}); // Passing null here silently fails
这个作品,但感觉不对初始化一个新的长[]
只是为了禁止震动。
This works but doesn't feel right to initialize a new long[]
just to disable the vibration.
这篇关于禁用振动的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!