Android 访问振铃器模式在服务内部不起作用

Android 访问振铃器模式在服务内部不起作用,android,notifications,Android,Notifications,我有一个应用程序,它可以在收到特定文本时创建一个带有声音的通知。为此,我检测当前ringerMode,将ringerMode更改为normal,播放声音,然后快速将ringer恢复到其原始设置。代码如下: 公共静态音频管理器音频 audio = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE); ringMode = audio.getRingerMode(); if (ringMode < 2

我有一个应用程序,它可以在收到特定文本时创建一个带有声音的通知。为此,我检测当前ringerMode,将ringerMode更改为normal,播放声音,然后快速将ringer恢复到其原始设置。代码如下:

公共静态音频管理器音频

    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);
        }
audio=(AudioManager)this.getSystemService(Context.audio\u服务);
ringMode=audio.getRingerMode();
如果(振铃模式<2)
//检查手机铃声是否设置为静音或振动
音频设置模式(2);
//以一种模式振动手机
长[]模式={0,25};
//长[]模式={0,1000,100,100,100,100,100,1000};
v、 振动(模式-1);
最终整数HELLO_ID=1;
mNotificationManager.notify(HELLO\u ID,notification);
//延迟返回到原始音量,以便可以听到通知
Handler=newhandler();
handler.postDelayed(新的Runnable(){
public void run(){
音频。设置铃声模式(铃声模式);
}  
}, 4000);
}
这在活动中完全可以正常工作,但在该通知服务中则不行,并且当前放在onStart方法中。 使用权限android:name=“android.permission.MODIFY\u AUDIO\u SETTINGS”也已添加到清单中。有什么想法吗?
谢谢

这并不是问题的真正解决方案,但我意识到有一个我应该调整的通知卷流。使用几乎完全相同的代码,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);
        }

这并不是这个问题的真正解决方案,但我意识到有一个通知卷流我应该一直在调整。使用几乎完全相同的代码,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);
        }