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

Sou*_*der 11 android android-notifications android-vibration

我在收到通知时为特定功能定义了自定义振动.

但是,当手机屏幕关闭时,自定义振动会伴随默认通知振动.

我尝试将手机置于静音模式,并以编程方式将其从静音模式中删除,并尝试使用部分唤醒锁,怀疑当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);
Run Code Online (Sandbox Code Playgroud)

请让我知道如何以编程方式禁用/启用默认通知振动.

Ass*_*ous 7

试着这样做

NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this);
Run Code Online (Sandbox Code Playgroud)

这将导致振动模式使用通知生成器并移除设置振动,这将禁用振动

这是我处理声音和振动的例子:

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());
Run Code Online (Sandbox Code Playgroud)


alb*_*eee 0

干得好

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

public void disableVibration(NotificationCompat.Builder builder){
    builder.setVibrate(new long[]{0L});
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。