Я хочу, чтобы пользователи настраивали свое приложение, выбирая звук уведомления, но я не знаю, как составить список звука и как установить их в качестве звука уведомления. Я искал и пробовал разными способами, но это не сработало, пожалуйста, помогите мне!
Это мой код для отображения уведомлений:
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), channelID)
.setSmallIcon(R.drawable.logo) /// Set notification's icon
.setSound(uri) /// Set sound
.setAutoCancel(true) /// Allow sound to auto cancel
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000}) /// Set sound vibration
.setOnlyAlertOnce(true) /// Only alert Once
.setContentIntent(pendingIntent) /// Set intent it will show when click notification
.setContent(getCustomDesign(title, message)); /// Set the design of notification
/// Manage notification
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
/// Check if android version is Oreo or upper to show notification via NotificationChannel
/// For each channel, you can set the and auditory behavior that is applied to all notifications in that channel
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
/// Set channel's ID, name, and importance
NotificationChannel notificationChannel = new NotificationChannel(channelID, "DEMO", notificationManager.IMPORTANCE_HIGH);
/// Set sound for channel
notificationChannel.setSound(uri, null);
/// Create channel
notificationManager.createNotificationChannel(notificationChannel);
}
/// Show the notification
notificationManager.notify(0, builder.build());
Я пытался создать RingTone Средство выбора:
btnChange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select notification tone");
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri) null);
startActivityForResult(intent, 5);
}
});
и onActivityResult:
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == NotificationActivity.RESULT_OK && requestCode == 5) {
Uri uri = intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
if (uri != null) {
//NotificationActivity.this.chosenRingtone = uri.toString();
RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_NOTIFICATION, uri);
} else {
//this.chosenRingtone = null;
}
}
}
Но Logcat сообщил, что моему проекту не предоставлено это разрешение: android .permission.WRITE_SETTINGS.