需要加入这个权限:
<uses-permission android:name=\"android.permission.ACCESS_NOTIFICATION_POLICY\"/>
代码:
/** * 设置铃声为静音,媒体音量不变(免打扰) * */ setDisturb() { // #ifdef APP-PLUS var pf = uni.getSystemInfoSync().platform; if (pf == "android") { let AudioManager = plus.android.importClass('android.media.AudioManager'); let Settings = plus.android.importClass('android.provider.Settings'); let Intent = plus.android.importClass('android.content.Intent'); let NotificationManager = plus.android.importClass('android.app.NotificationManager'); let Context = plus.android.importClass('android.content.Context'); let main = plus.android.runtimeMainActivity(); this.audioManager = main.getSystemService(Context.AUDIO_SERVICE); if (this.audioManager == null) { return; } //console.log(AudioManager.RINGER_MODE_SILENT);//0 //console.log(AudioManager.RINGER_MODE_NORMAL);//2 //console.log(AudioManager.RINGER_MODE_VIBRATE);//1 let n = main.getSystemService(Context.NOTIFICATION_SERVICE); if (n == null) { console.log("n is null"); return; } //授权 if (n.isNotificationPolicyAccessGranted()) { this.audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT); uni.showToast( { title: '已为您开起免打扰模式。', duration: 3000, icon: 'none', mask: false }); } else { uni.showToast( { title: '稍后,请授予此应用免打扰权限,然后请再按一次以开启免打扰。', duration: 4500, icon: 'none', mask: false }); setTimeout(function() { let intent = new Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS); main.startActivity(intent); }, 4500); } this.audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT); var n1 = this.audioManager.getRingerMode() } // #endif },
标签:uniapp,AudioManager,app,audioManager,plus,importClass,let,android From: https://www.cnblogs.com/HelloQLQ/p/17363098.html