如何仅在 Android 中发生通知时以编程方式更改音频模式?

问题描述 投票:0回答:2

我想问如何在有通知时临时更改音频模式?

例如,我希望我当前的模式是静音、没有声音、没有振动。当通知发生时,音量最大并且有振动。通知完成后,我想将其改回静音模式,没有声音和振动。

问题是,我可以从静音更改为最大音量并带有振动,但通知完成后音频设置仍然是最大声音并带有振动。

这是我的代码:

AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION,
                             audioManager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION),
                             AudioManager.FLAG_ALLOW_RINGER_MODES);

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(GCMHandler.MESSAGE_NOTIFICATION_ID,
                           NotificationBuilder.build(context,
                                                     "Mohon konfirmasi booking #" + bookings.last().getCode(),
                                                     "Booking #"+bookings.last().getCode()+" belum dikonfirmasi!",
                                                     NotificationBuilder.Type.PUSH_NOTIFICATION,
                                                     R.drawable.notif_icon, 0xFFC200));

如果我将

audioManager.setRingerMode(RINGER_MODE_SILENT)
放在
notificationManager.notify()
之后,则通知将以完全静音模式构建

问候,

java android android-studio android-audiomanager
2个回答
0
投票

** 我认为此代码将有助于手机中的默认铃声 如果您处于静音模式,它会振动,否则默认通知音

Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.piconebg)
            .setContentTitle(title)
             .setSubText(title3)
            .setContentText(message)
            .setAutoCancel(true)
            .setTicker(title)
            .setDefaults(Notification.DEFAULT_LIGHTS)
         .setColor(color)
            .setStyle(new       NotificationCompat.BigTextStyle().bigText(title))
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
            .setWhen(System.currentTimeMillis())
            .setLargeIcon(BitmapFactory.decodeResource(PushNotificationService.this.getResources(), R.drawable.push))
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);
    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(id /* ID of notification unique */,notificationBuilder.build());

0
投票

我也想解决这个问题。您可以使用删除意图将模式设置回相关的广播接收器中:

private fun createNotification(context: Context, channelId: String) {
    val intent = Intent("my.package.notification_closed_action")
    intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES)
    intent.putExtra("notification_id", noticationId)
    intent.setPackage(context.packageName)
    val deleteIntent = PendingIntent.getBroadcast(context, 123, intent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
    
    val notificationBuilder = Notification.Builder(context, channelId)
       ...
       .setDeleteIntent(deleteIntent)
       ...
}
© www.soinside.com 2019 - 2024. All rights reserved.