由于Android O及更高版本,我完全理解,没有简单的方法可以通过应用代码自定义通知声音。
通常的方法是调用Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS
private void showNotificationSoundListPreferenceDialogFragmentCompat(Preference preference) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, getContext().getPackageName());
intent.putExtra(Settings.EXTRA_CHANNEL_ID, com.yocto.wenote.reminder.Utils.createNotificationChannel());
try {
startActivity(intent);
} catch (android.content.ActivityNotFoundException e) {
Log.e(TAG, "", e);
trackEvent("showNotificationSoundListPreferenceDialogFragmentCompat", "fatal", e.getMessage());
}
}
}
看起来像是
但是,我注意到市场上有一些应用程序,确实提供了动态调整通知音量的方法。
我可以知道,实现此目标的方法是什么?
答案 0 :(得分:4)
这仅仅是一个hack,一种实现它的方法,它将实现您所需的行为。在我的应用程序中,定位API 26;我已经实施了声音和振动自定义功能,但是是手动进行的。
NotificationCompat.Builder notification = new NotificationCompat.Builder(this, "Channel1");
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Vibrations
Vibrator vib = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
vib.vibrate(VibrationEffect.createWaveform(new long[] {200,300,500,100,100}, 1));
//Sound
Intent soundIntent = new Intent(this, PlaySound.class);
serviceIntent.setAction("ACTION_START_PLAYBACK");
serviceIntent.putExtra("UriSound", soundUri.toString());
context.startForegroundService(soundIntent);
// if notification is outted, just delete notification; thus delete intent
Intent outNotification = new Intent(context, PlaySound.class);
deleteIntent.setAction("ACTION_STOP_PLAYBACK");
PendingIntent pendingOutNotification =
PendingIntent.getService(this, 0, outNotification, 0);
builder.setDeleteIntent(pendingOutNotification);
} else {
builder.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
builder.setSound(uri);
}
notificationManager.notify(0, builder.build());
在您的PlaySound.class extend service
中,在media player
上播放声音,然后根据特定intent
的{{1}}控制音量或特定声音... >
根据从滑块输入的用户来确定音量或uri,然后通过channel
发送或使用intent
s将其保存为键值对。
答案 1 :(得分:2)
您可以通过执行以下步骤来实现 检查适当的权限
isNotificationPolicyAccessGranted()
返回布尔值,并且是NotificationManager的一部分。 必须安装Android N或更高版本,因为它可以修复“请勿打扰” 如果未提供,则明确请求权限
Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS
在活动中设置AudioManager 使用滑块创建自定义视图,以获取 getStreamMinVolume(5)和 getStreamMaxVolume(5)
之间的整数然后用
设置所需的音量setStreamVolume(5, yourdesiredvalue in int , 0)
我希望它将对您有帮助!