首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法在Android 8中禁用通知振动

无法在Android 8中禁用通知振动
EN

Stack Overflow用户
提问于 2018-01-15 18:29:53
回答 5查看 11.3K关注 0票数 10

当显示通知时,我尝试禁用vibration

Func:

代码语言:javascript
运行
复制
public static Notification buildNotifForUploaderService(Context ctx, String title, String message) {

        Notification notification;
        NotificationCompat.Builder notificationBuilder;

        //If device is Android 8+
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
            //setting pattern to disable vibrating
            notificationChannel.setVibrationPattern(new long[]{0L});
            notificationBuilder = new NotificationCompat.Builder(ctx, CHANNEL_ID);
        } else {
            notificationBuilder = new NotificationCompat.Builder(ctx);
            notificationBuilder.setVibrate(new long[]{0L});
        }


        notificationBuilder
                .setContentTitle(title)
                .setContentText(message)
                .setLargeIcon(BitmapFactory.decodeResource(ctx.getResources(), R.mipmap.ic_launcher))
                .setSmallIcon(R.drawable.ic_backup_black_24dp);


        notification = notificationBuilder.build();

        return notification;
    }

我在活动的onCreate()上调用它,如下所示:

代码语言:javascript
运行
复制
Notification notification = NotificationHelper.buildNotifForUploaderService(this, "title", "message");
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(1, notification);

这是仍然振动的。我在Android8设备上进行了测试。我也试过

代码语言:javascript
运行
复制
 notificationChannel.setVibrationPattern(null);

仍然不起作用。

我有过

代码语言:javascript
运行
复制
<uses-permission android:name="android.permission.VIBRATE" />

不管我如何定义振动模式,如下所示:

代码语言:javascript
运行
复制
new long[]{1000L, 500L, 300L, 1000L};

振动与我的设置不符。Onyl默认的“两短”振动发生。

如果可以的话请帮忙,提前谢谢。

E D I T:

正如Avijit Karmakar提到的,我已经添加了

代码语言:javascript
运行
复制
  notificationChannel.enableVibration(false);

完整代码现在:

代码语言:javascript
运行
复制
public class MainActivity extends AppCompatActivity {

    final static String CHANNEL_ID = "MY_CHANNEL_ID";
    final static String CHANNEL_NAME = "MY_CHANNEL_NAME";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Notification notification;
    NotificationCompat.Builder mBuilder;
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
        //Disabling vibration!
        notificationChannel.enableVibration(false);
        notificationManager.createNotificationChannel(notificationChannel);
        mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID);

    } else {
        mBuilder = new NotificationCompat.Builder(this);
        mBuilder.setVibrate(new long[]{0L});
    }

    mBuilder.setContentTitle("title")
            .setContentText("message")
            .setSmallIcon(R.drawable.ic_android_black_24dp);

    Bitmap bm = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
    mBuilder.setLargeIcon(bm);

    notification = mBuilder.build();
    notificationManager.notify(1, notification);
    }
}

它还在震动。

我在小米A1 (安卓8.0)上测试过

有没有人可以试一下这段代码,并帮助我得出结果?

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48261118

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档