如何在语音运行时保持通知?
主要活动:
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);发布于 2017-05-31 15:02:01
发布于 2017-05-31 16:26:20
这对我有用。
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);
}
});
}因为应用程序停止工作,所以需要新的处理程序。
https://stackoverflow.com/questions/44272952
复制相似问题