2015-11-28 68 views
-3

我想檢查是否正在使用我的應用程序播放音樂。我已經完成了我的應用程序,它只能使用一次。但我想知道我們如何重複檢查這一點。如何反覆檢查音樂播放器是否處於活動狀態?

這是我的代碼。但是這個代碼只是檢查音樂是否活動或不活動一次。

if(mAudioManager.isMusicActive()) { 

     Toast.makeText(this, "Audio is playing", Toast.LENGTH_LONG).show(); 
     Intent srvc = new Intent(this, OverlayShowingService.class); 
     startService(srvc);  
     try { 
      Timer timer = new Timer(); 
      timer.wait(1000); 
     } catch (Exception e) { 

      System.out.println("Exception found"); 
     } 

} 
+2

嗨,歡迎來到Stack Overflow。可能是一個想法,用一些關於**你試過的**和**你有什麼問題**的更多信息來格式化你的問題。另外,請不要隨意CAPITALIZE字樣,我們不是聾啞人。 – ScottMcGready

+0

確定@scottmcgready,我已經改變了,正如你所說的。 –

回答

0

方說ScottMcGready請跟隨他suggestopns

這是回答你的question-

//Declare the timer 
Timer t = new Timer(); 
//Set the schedule function and rate 
t.scheduleAtFixedRate(new TimerTask() { 

    @Override 
    public void run() { 

      //here you can set check of players active ness 

     //Called each time when 2000 milliseconds (2 second) 
    } 

}, 
//Set how long before to start calling the TimerTask (in milliseconds) 
0, 
//Set the amount of time between each execution (in milliseconds) 
1000); 

,如果你想取消任務只需調用t.cancle()

+0

非常感謝你@sud。我可以使用廣播接收器來實現嗎? –

相關問題