出现音乐播放器通知时禁用振动

Disable vibrating when a music player notification appears

如何在出现通知时禁用振动?

我在PlayerService class中使用了startForground(int, int);

有什么解决办法吗?

注意:我尝试了很多方法,但它又开始振动了。

builder.setDefault(Notification.DEFAULT_ALL);

builder.setVibrate(new long[]{0L})

uilder.setVibrate(new long[]{01, 01})

 NotificationManager notificationManager;
    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationChannel channel;
    builder = new NotificationCompat.Builder(this);
    String title = playlist().get(play_song_id).get(1);
    String content = getSongArtists();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
        channel = new NotificationChannel("lawanmusic", "lawanmusic", NotificationManager.IMPORTANCE_HIGH);
        channel.setLightColor(getColor(R.color.colorAccent));
        channel.enableVibration(false);
        channel.setVibrationPattern(new long[]{ 01, 01 });
        channel.enableVibration(false);
        channel.setImportance(NotificationManager.IMPORTANCE_DEFAULT);
        builder.setChannelId(channel.getId());
        notificationManager.createNotificationChannel(channel);
    }
    builder.setDefaults(Notification.DEFAULT_ALL | -Notification.DEFAULT_VIBRATE).setVibrate(new long[]{ 0L });
    builder.setStyle(new android.support.v4.media.app.NotificationCompat.MediaStyle().setShowActionsInCompactView(0,1,2).setCancelButtonIntent(null).setMediaSession(new MediaSessionCompat(this, "media").getSessionToken()).setShowActionsInCompactView());
    builder.setContentTitle(title);
    builder.setContentText(content);
    @DrawableRes int icon = R.drawable.play_icon_notification;
    builder.setSmallIcon(icon);
    builder.setLargeIcon(getSongCover());
    PendingIntent pendingIntent = PendingIntent.getActivity(this, Constants.NOTIFICATION_ID, new Intent(this, ActivityMain.class).putExtra("player", true), PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(pendingIntent);
    final PendingIntent prev = PendingIntent.getBroadcast(this, Constants.NOTIFICATION_ID, new Intent(this, PlayerReceiver.class).setAction(PREV_ACTION), PendingIntent.FLAG_UPDATE_CURRENT);
    builder.addAction(R.drawable.notification_prev, "Prev", prev);
    final PendingIntent pause = PendingIntent.getBroadcast(this, Constants.NOTIFICATION_ID, new Intent(this, PlayerReceiver.class).setAction(PAUSE_ACTION), PendingIntent.FLAG_UPDATE_CURRENT);
    builder.addAction(R.drawable.notification_pause, "Pause", pause);
    final PendingIntent next = PendingIntent.getBroadcast(this, Constants.NOTIFICATION_ID, new Intent(this, PlayerReceiver.class).setAction(NEXT_ACTION), PendingIntent.FLAG_UPDATE_CURRENT);
    builder.addAction(R.drawable.notification_next, "Next", next);
    final PendingIntent stop = PendingIntent.getBroadcast(this, Constants.NOTIFICATION_ID, new Intent(this, PlayerReceiver.class).setAction(STOP_ACTION), PendingIntent.FLAG_UPDATE_CURRENT);
    builder.addAction(R.drawable.notification_stop, "Player Off", stop);
    builder.setShowWhen(false);
    builder.build();
    notification.flags = Notification.DEFAULT_ALL | -Notification.DEFAULT_VIBRATE;
    notification.vibrate = null;
    notification = builder.build();
    startForeground(Constants.NOTIFICATION_ID, notification);

您可以使用 IMPORTANCE_DEFAULT 创建 NotificationChannel,文档中说:

Default notification importance: shows everywhere, makes noise, but does not visually intrude.

你可能想要 IMPORTANCE_LOW:

Low notification importance: shows everywhere, but is not intrusive.

或IMPORTANCE_MIN:

No sound and does not appear in the status bar

但我不能确定,因为我还没有看到您关于此服务的所有代码

Create and Manage Notification Channels