[英]How to set device to silent mode without vibration
我的應用程序具有某些功能,我需要將設備設置為無振動的靜音模式。 這是由通知到達觸發的。
我使用以下代碼來做到這一點:
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
這在設備屏幕打開時有效,但在設備屏幕關閉(閑置時)一段時間時不起作用。
有什么辦法可以使這項工作每次都有效,即使手機屏幕處於關閉狀態?
嘗試這個
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());
這可以通過非常簡單的方式完成。 首先,您需要獲取android音頻管理器的參考
AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
然后使用AudioManager的引用調用AudioManager的方法。
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
此代碼會將電話的振鈴模式設置為無振動的靜音。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.