2017-05-30 18 views
0

如何在語音運行時保持通知?如何關閉語音完成後的通知

主要活動:

TextToSpeech tts; 

... 

public void notification() { 
NotificationCompat.Builder notification = 
new NotificationCompat.Builder(this) 
.setSmallIcon(R.drawable.ic_volume_up_white_36dp) 
.setOngoing(true) 
NotificationManager NotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
NotifyMgr.notify(1, notification.build()); 
} 

... 

public void speak() { 
    tts=new TextToSpeech(getApplicationContext(),new TextToSpeech.OnInitListener() { 
     @Override 
     public void onInit(int status) { 
      notification(); //Call Notification 
      tts.setLanguage(Locale.US); 
      tts.speak("message", TextToSpeech.QUEUE_FLUSH, null); 
     } 
    }); 
} 

什麼是貫徹落實通知需要取消行動,並應在何處進入

NotifyMgr.cancel(1); 

回答

0

使用方法isSpeaking方法來檢查TTS是否正在講話。

if(!isSpeaking()){ 
notifymgr.cancel(1); 
} 

Reference

+0

我收到未知方法'isSpeaking' – Mssjim

+0

適用於(!tts.isSpeaking)。 – Mssjim

+0

太好了。 isSpeaking是布爾方法TextToSpeech類。如果TTS引擎正在講話,該方法返回true。和那個服務器你的要求我猜。 – iDevRoids

0

這對我的作品。

public void speakNotification() { 
    new Handler().postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      NotificationManager NotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
      if (tts.isSpeaking()) { 
       NotifyMgr.notify(3, notificationSpeak.build()); 
      } else { 
       speakNotification(); 
      } 
      if(!tts.isSpeaking()) { 
       NotifyMgr.cancel(3); 
      } else { 
       speakNotification(); 
      } 
     } 
    }, 1); //1 is the update time 
} 

public void speakMotd() { 
    tts=new TextToSpeech(getApplicationContext(),new TextToSpeech.OnInitListener() { 
     @Override 
     public void onInit(int status) { 
      speakNotification(); 
      tts.setLanguage(Locale.US); 
      tts.speak("message", TextToSpeech.QUEUE_FLUSH, null); 
     } 
    }); 
} 

需要新的處理程序,因爲應用程序停止工作。