2010-07-09 25 views
2

我有一個應用程序,它會在特定文本進入時創建聲音通知。爲此,我檢測當前的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

不是真的解決這個問題,但我意識到有一個通知卷流,我應該已經調整。通過相同的代碼,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); 
     }