android "Use ringtone volume for notifications"

2.8k Views Asked by At

I'm trying to change the volume of notification stream (not ring) on a HTC One with android 4.3. I'm using this code:

int setting = Settings.System.getInt(getContentResolver(), "notifications_use_ring_volume", -20);
boolean b = Settings.System.putInt(getContentResolver(), "notifications_use_ring_volume", 1 - setting);

so everytime b is true(meaning that the change has been made) and everytime I'm putting 0 or 1. Now, after I'm changing the value, when I read it, it displays the value that I've updated in the avove code, but when I'm going to Settings->Sound->Volumes, the

"Use ringtone volume for notifications"

checkbox remains unchanged (when that setting is 0 or 1). Am I missing something here? How to update this setting from code so I can see in the phone's settings this change?

3

There are 3 best solutions below

1
On

Try this.

AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
am.setStreamVolume(AudioManager.STREAM_NOTIFICATION, am.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION), AudioManager.FLAG_PLAY_SOUND);

You can control volume, to change second parameter. (am.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION))

setStreamVolume(int, int, int)

1
On

Create a "raw" folder inside your application resource folder and place the ringtone(.mp3) file inside that raw folder and then add this line when you receive the notification.

notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.sapphire);

OR

int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
notification.sound = Uri.parse("android.resource://"
            + context.getPackageName() + "/" + R.raw.sapphire);

And then create and your intent and pending Intent and call this

notification.setLatestEventInfo(context, message, title, pendingIntent);
notificationManager.notify(0, notification);

3
On

You need to use AudioManager. It has a method named, setStreamVolume() which helps you manipulate volume for Audio streams.

Here's a sample application which demonstrates what you want. Link to sample applcation.