Android 禁用通知的振动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24008764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:44:01  来源:igfitidea点击:

Disable vibration for a notification

androidnotificationsvibrationandroid-vibration

提问by nstCactus

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.

我正在使用通知编写应用程序。Google 开发人员指南鼓励开发人员提供自定义通知的设置(禁用振动、设置通知声音...),因此如果用户以这种方式设置,我将尝试禁用通知的振动。

I am using NotificationCompat.Builderto create the notification, like this:

我正在使用NotificationCompat.Builder创建通知,如下所示:

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.

但是当通知出现时手机仍然振动。

How can I disable vibration for notifications?

如何禁用通知的振动?

回答by nstCactus

After a long trial & error session, I think I finally understood what's wrong.

经过长时间的反复试验,我想我终于明白出了什么问题。

The problem lies in this instruction notificationBuilder.setDefaults(Notification.DEFAULT_ALL).

问题出在这个指令上notificationBuilder.setDefaults(Notification.DEFAULT_ALL)

No matter what parameter you pass to notificationBuilder.setVibrate()after setting DEFAULT_ALLor DEFAULT_VIBRATEwill be silently discarded. Someone at Google must have decided to give a higher precedence to setDefaultsthan to setVibrate.

无论你notificationBuilder.setVibrate()在设置后传递给什么参数,DEFAULT_ALL否则DEFAULT_VIBRATE都会被默默丢弃。谷歌的某个人一定已经决定给予setDefaults比 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.

这有效,但long[]为了禁用振动而初始化一个新的感觉不正确。

回答by Jakob

In the year 2020:

2020年:

Setting the importance of the notification channel to NotificationManager.IMPORTANCE_NONEworked for me.

设置通知渠道NotificationManager.IMPORTANCE_NONE对我有用的重要性。

回答by Oubaida AlQuraan

They are not stop because you are use "setDefaults(Notification.DEFAULT_ALL)"so if you need to stop vibration and sound remove this line , or if you need to use the default sound and stop vibration I think you must use setDefaults(Notification.DEFAULT_SOUND)etc ...

它们不会因为您正在使用而停止,"setDefaults(Notification.DEFAULT_ALL)"因此如果您需要停止振动和声音,请删除此行,或者如果您需要使用默认声音并停止振动,我认为您必须使用setDefaults(Notification.DEFAULT_SOUND)等...

回答by ban-geoengineering

.setVibrate(null)works for me - and a better solution than creating a needless long[].

.setVibrate(null)对我有用 - 比创建不必要的 long[] 更好的解决方案。

Result: device doesn't vibrate and no grumbling in LogCat either. :)

结果:设备不振动,LogCat 中也没有抱怨。:)

回答by Houssin Boulla

notification.vibrate = new long[] { -1 };

this code work for me.

这段代码对我有用。

回答by Bipin

private void removeSoundAndVibration(Notification notification) {
        notification.sound = null;
        notification.vibrate = null;
        notification.defaults &= ~DEFAULT_SOUND;
        notification.defaults &= ~DEFAULT_VIBRATE;

This code is from Notification Compat Api Class. This should work, add all these to your builder.

此代码来自 Notification Compat Api 类。这应该有效,将所有这些添加到您的构建器中。