如何禁用/重新启用接收通知时发生的震动?

11

当我收到通知时,我正在定义一个特定功能的自定义振动。

然而,当手机屏幕关闭时,自定义振动会与默认通知振动一起播放。

我尝试将手机静音并以编程方式取消静音,还尝试使用部分唤醒锁定,怀疑当CPU关闭时,也会触发默认振动,但这两种方法似乎都不起作用。

我还尝试静音默认音频和振动,并在收到通知后恢复它们,但这只有助于掩盖默认通知声音,不能消除默认震动。

//Trying to mute the notification sound and vibration
audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, 0, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
 //Restoring the defaults
audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, defaultNotifVolume, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
请告诉我如何通过编程来启用/禁用默认通知震动。
5个回答

8

尝试做这件事

NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this);

这将导致震动模式。使用通知生成器并删除设置震动,这将禁用震动。
以下是我处理声音和震动的示例: ex:
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentText(body)
                .setContentTitle(title)
                .setSound(soundUri)
                .setLargeIcon(icon)
                .setSound(soundUri)
                .setLights(Color.parseColor("#ffb400"), 50, 10)
                .setVibrate(new long[]{500, 500, 500, 500})
                .setAutoCancel(true)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notificationBuilder.build());

1
这是不可能的(也许需要root权限?)。
应用程序不能干扰其他应用程序,因此即使可能,也会违反Google Play规则。

1
我猜您想切换手机的铃声模式为振动、响铃或者静音。

为了实现通知,需要在通知之前将RINGER_MODE设置为SILENT,并在通知后将其设置回NORMAL

在Android中,您需要获得更改设备设置的权限才能切换任何设置参数。首先,在清单文件中添加以下内容:

<uses-permission android:name="android.permission.WRITE_SETTINGS"/>

现在,使用AudioManager来实现您想要的功能,

AudioManager aManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
aManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);

你可以选择 RINGER_MODE_VIBRATERINGER_MODE_SILENTRINGER_MODE_NORMAL
同时,在清单文件中添加权限 android.permission.VIBRATE

看起来你和我想得一样。这与我尝试过的类似,但是这种方法有两个问题。首先,这不是一个干净的解决方案,因为代码会干扰系统操作(如果我们将手机设置为静音模式...一旦收到通知...手机将处于正常模式),其次...当我将其设置回正常或振动模式时...会伴随着相应的声音和振动模式转换...这会干扰我设置的自定义振动...而这正是我想要避免的。 - SoulRayder
@SoulRayder 那我建议你在通知期间取消系统震动, Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 然后 vibration.cancel();。这样就可以取消系统震动,然后生成自定义的震动。 - Q2x13
这正是我的问题 :) - SoulRayder
@SoulRayder,另外尝试使用notificationBuilder.setVibrate(new long[] { -1 });并将类型设置为仅声音,setDefaults(Notification.DEFAULT_SOUND)而不是DEFAULT_ALL。我假设您正在使用NotificationBuilder - Q2x13
不,我没有使用那个...即使是这样,它也只能控制自定义通知震动行为,而不能控制系统通知行为。 - SoulRayder

0

给你

public void enableVibration(NotificationCompat.Builder builder, long[] customPattern){
    builder.setVibrate(customPattern);
}

public void disableVibration(NotificationCompat.Builder builder){
    builder.setVibrate(new long[]{0L});
}

希望这能有所帮助。

1
这是针对我们构建的自定义通知。我想问一下如何覆盖系统通知震动并禁用/屏蔽和重新启用它。 - SoulRayder
你所问的是如何在安卓系统中禁用/启用震动?据我所知,安卓框架API没有选项可以为所有应用程序禁用所有震动。有人制作了一个可以在整个系统中关闭它们的应用程序 - https://play.google.com/store/apps/details?id=sk.blade.globalvibratetoggle注意:需要root手机,因为没有API可以禁用震动。希望这可以帮到你。 - albeee

0

尝试这个来禁用接收通知的振动

notificationBuilder.setDefaults(Notification.DEFAULT_LIGHT | Notification.DEFAULT_SOUND)
                   .setVibrate(new long[]{0l}); // Passing null here silently fails

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接