2016-05-05 78 views

回答

-1

這就是我如何執行這個任務。首先我捕捉最初的設置。畢業後,一旦用戶解除通知,我會撥打另一個功能返回設置。下面是即將畢業的聲音代碼:

public static void graduateVolumeLevel(Context context) { 
    getVolumeLevel(context); 
    final AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); 
    final int DELAY_UNTIL_NEXT_INCREASE = 6 * 1000; 

    LaunchService.startThread(); 

    mHandlerIncreaseVolume = new Handler(appService.thread.getLooper()); 
    /*you may not need this part. 
    I use it b/c i run my stuff on another thread not to 
    interfere with the main thread. 
    Creating a new handler on main thread may be sufficient for your needs.*/ 

    if(runnable==null) { 
     runnable = new Runnable() { 
      @Override 
      public void run() { 
       int currentSystemVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_SYSTEM); 
       int currentRingVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_RING); 

       if (currentSystemVolume != mAudioManager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM)|| 
         currentRingVolume != mAudioManager.getStreamMaxVolume(AudioManager.STREAM_RING)) { //if not max 
        //increase the volume of the alarm stream 
        mAudioManager.setStreamVolume(AudioManager.STREAM_RING,currentRingVolume++,0); 
        mAudioManager.setStreamVolume(AudioManager.STREAM_SYSTEM, currentRingVolume++, 0); 

        if(mHandlerIncreaseVolume!=null) 
        mHandlerIncreaseVolume.postDelayed(this, DELAY_UNTIL_NEXT_INCREASE); 
       } 
      } 
     }; 
    } 

    mHandlerIncreaseVolume.post(runnable); 
} 

我發現了一個類似的帖子,但無法再找到它引用它。這是我用來創建我的代碼的東西,稍微修改一下,因爲這個例子並沒有按照原樣工作。同樣,一旦我返回原始設置,我從處理程序中刪除回調並將其設置爲null。