2017-05-30 130 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); 
     } 
    }); 
} 

需要新的处理程序,因为应用程序停止工作。