语音结束后如何关闭通知

时间:2017-05-30 22:34:28

标签: java android

如何在语音运行时保留通知?

主要活动:

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);

2 个答案:

答案 0 :(得分:0)

使用Method isSpeaking Method检查tts是否正在发言。

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

Reference

答案 1 :(得分: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);
        }
    });
}

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