【问题标题】:How to set device to silent mode without vibration如何将设备设置为静音模式而不振动
【发布时间】:2018-01-02 00:29:55
【问题描述】:

我的应用程序具有某些功能,我需要将设备设置为静音模式而不振动。这是由通知的到达触发的。

我使用以下代码来做到这一点:

AudioManager audioManager =  (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);

这在设备屏幕打开时有效,但在设备屏幕关闭(空闲时)一段时间后多次无效。

有没有办法让这项工作每次,即使手机屏幕关闭?

【问题讨论】:

    标签: android android-audiomanager


    【解决方案1】:

    试试这个

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

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

    处理声音和振动的示例:

    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());
    

    【讨论】:

    • 感谢您的回答。但是,问题是,我没有创建通知,也无法控制它。当我收到来自其他应用程序的传入通知时,我必须强制执行上述功能。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-06
    • 2021-07-13
    • 1970-01-01
    • 2011-03-24
    • 1970-01-01
    • 2012-07-09
    相关资源
    最近更新 更多