我有一个应用程序,当特定文本进入时会创建声音通知。为此,我检测当前的ringerMode,将ringerMode更改为正常,播放声音,然后快速将铃声恢复为原始状态设置。这是代码:
public static AudioManager audio;
audio = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
ringMode = audio.getRingerMode();
if (ringMode < 2)
//Checks to see if the phones ringer is set to silent, or vibrate
audio.setRingerMode(2);
//vibrates phone in a pattern
long[] pattern = {0, 25};
//long[] pattern = {0, 1000, 100, 100, 100, 100, 100, 1000};
v.vibrate(pattern, -1);
final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
//delays return back to original volume so notification can be heard
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
audio.setRingerMode(ringMode);
}
}, 4000);
}
这在活动中完全正常,但在此通知服务中没有,并且当前放在onStart方法中。 使用权限android:name =“android.permission.MODIFY_AUDIO_SETTINGS”也已添加到清单中。有任何想法吗? 感谢
答案 0 :(得分:0)
不是解决这个问题的方法,但我意识到我应该调整一个Notification卷流。使用完全相同的代码,Notify类现在可以正常工作。
if (startSound)
{
//This sound plays using the ringer volume
//notification.sound = Uri.parse("android.resource://itp.uts.program/" + R.raw.pager);
//Sets ringer temporarily to active, so notification is heard
audio.setStreamVolume(5, 3, 0);
//vibrates phone in a pattern
long[] pattern = {0, 25};
//long[] pattern = {0, 1000, 100, 100, 100, 100, 100, 1000};
v.vibrate(pattern, -1);
}
//Creates the notification
mNotificationManager.notify(HELLO_ID, notification);
if (startSound)
{
//delays return back to original volume so notification can be heard
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
audio.setStreamVolume(5, streamVolume, 0);
}
}, 7000);
}