0

In my Android application I use TTS and Voice Recognition. Despite using all the available callbacks to understand the status they are in, I still get situations that I cannot handle. Examples:

1) TTS reports successful initialisation, but fails to speak (known issue for some custom TTS engines (mentioning no names)).

2) Network access is lost after initial successful connection, which can result in a huge amount of lag before a network error is returned.

There are other situations that I won't bore you with.

I've read and digested time monitoring threads such as this and this so I understand how to accurately determine the time between X&Y, but it's a monitoring method that I'm after, which I can react to when a set limit is reached.

Excuse my pseudo-code, but I hold my hands up, I've no idea where to begin on this..

    public boolean methodSuccess = false;

    public void troublesomeMethod() {
    if(// everything completed and I got a usual result) {
    methodSuccess = true;
    } 

    public boolean timeMonitor(int maxDuration) {

    // use System.currentTimeMillis() here as start value

    // monitor time elapsed here in loop
    // monitor methodSuccess in loop
    // return false if methodSuccess is set to true whilst in loop

    // break if maxDuration reached and return true

    }

Again, excuse the above code, I simply can't think how to achieve this...

In addition, if I started the above with:

    boolean monitorThis = timeMonitor(5000); // let me know when 5 seconds have passed
    startTroublesomeMethod(); // the one I want to monitor

Then it would be pointless if I had to 'wait' on the boolean response being returned before startTroublesomeMethod() actually began! Or the troublesomeMethod began before I started monitoring it...

    if(monitorThis) {
    // react to lagging method by destroying TTS or Listener Object
    } else {
    // the troublesomeMethod behaved normally - do nothing
    }

I hope in my confusion I've managed to express what I'm trying to achieve.

I thank you in advance for your help.

Community
  • 1
  • 1
brandall
  • 6,094
  • 4
  • 49
  • 103

1 Answers1

1

You can use a Handler to post a delayed check. For example:

    Handler mHandler = new Handler();
    mHandler.postDelayed(new Runnable() {
        @Override public void run() {
            // You can set a flag or if you run your method 
            // on another thread, you can interrupt that thread. 
        }
    }, 5000);

Keep in mind that the Handler is set to the same thread it was created in. If the method you are trying to monitor is running in the same thread, you may have problems. Call your method in another thread and keep a reference to that thread to interrupt it if necessary.

    Thread mThread = new Thread(new Runnable() {
        @Override public void run() {
            // Call your method here and keep a reference to this thread.
        }
    });
    mThread.run();
Chase
  • 11,161
  • 8
  • 42
  • 39
  • This looks great, thank you. It will take me a while to read up on how to keep a reference to a thread or to actually interrupt it, but I will return and mark your answer as correct once I've got my head around it (and it works of course!). Cheers – brandall Jul 17 '12 at 14:52