简体   繁体   中英

Monitor time elapsed for a process or method

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM