2017-05-30 5 views
0

Wie kann die Benachrichtigung nur während der Sprachausgabe beibehalten werden?So schließen Sie die Benachrichtigung, nachdem die Sprachausgabe beendet wurde

Hauptaktivität:

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

Was Mitteilung zur Umsetzung benötigt wird Aktion abbrechen und wo sie eingegeben werden sollten

NotifyMgr.cancel(1); 

Antwort

0

die Methode isSpeaking Methode verwenden, um zu überprüfen, ob tts gerade spricht.

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

Reference

+0

Ich bekomme Unbekannte Methode 'isSpeaking' – Mssjim

+0

Funktioniert mit (! Tts.isSpeaking). – Mssjim

+0

Großartig. isSpeaking ist eine boolesche Methode der TextToSpeech-Klasse. Die Methode gibt true zurück, wenn die TTS-Engine spricht. und dieser Server deine Anforderung, denke ich. – iDevRoids

0

Dies funktioniert für mich.

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

Neuer Handler wird benötigt, weil die App nicht mehr funktioniert.

Verwandte Themen