3

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?

4

3 回答 3

1

您需要使用AudioManager。它有一个名为setStreamVolume()的方法,可帮助您控制音频流的音量。

这是一个示例应用程序,它演示了您想要什么。链接到示例应用程序

于 2014-01-09T06:59:37.327 回答
0

尝试这个。

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

您可以控制音量,以更改第二个参数。(am.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION))

setStreamVolume(int, int, int)

于 2014-01-03T00:29:54.110 回答
-1

在您的应用程序资源文件夹中创建一个“原始”文件夹并将铃声(.mp3)文件放在该原始文件夹中,然后在您收到通知时添加此行。

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

或者

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);

然后创建你的意图和待处理的意图并调用它

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

于 2014-01-07T06:56:28.450 回答