-1

I have a problem with events schedulation. I need to change text color every fixed time. Range of this is between 100ms and 300ms. I've tried to use Android Timer - TimerTask but after 10-15 Thread the rendering is delayed then I lost real time update. I've tried also to use Thread.sleep() but I've had an exception with MainThreadActivity. Timer problem I think that is JVM Thread allocation or at most a concurrency problem. I asked you what is the best way for scheduling fast event with fixed delay in Android and if my approach is correct.

Thank you in advance.

Fabio

murdock
  • 49
  • 1
  • 3

2 Answers2

0

Try something like this:

final static int REFRESH_TIME_MS = 100;
final static int REFRESH_TIME_MS_2 = 200;

Runnable mPeriodicTask = new Runnable() {
    public void run() {
        //do what you want
    }
};

Runnable mPeriodicTask_2 = new Runnable() {
    public void run() {
        //do what you want
    }
};

@Override
public void onCreate(Bundle savedInstanceState) {
    ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
    executor.scheduleAtFixedRate(mPeriodicTask, 0, REFRESH_TIME_MS, TimeUnit.MILLISECONDS);
    executor.scheduleAtFixedRate(mPeriodicTask_2, 0, REFRESH_TIME_MS_2, TimeUnit.MILLISECONDS);
}
Denys Vasylenko
  • 2,135
  • 18
  • 20
  • Hi Denis, I need to schedule n-events not only one. This example show how I can schedule a single event or am I wrong? – murdock Dec 10 '13 at 15:40
  • You can shedule n-events. Edited answer to demonstrate this. – Denys Vasylenko Dec 10 '13 at 16:03
  • Denis, I know how many events I need to schedule only a runtime. Is not a fixed amount. I need to implements a mechanism that schedule a variable number of events. – murdock Dec 10 '13 at 16:30
  • You can in any time add another one and remove already added. So what's problem? Provide your current implementation to clarify question. – Denys Vasylenko Dec 10 '13 at 18:01
0

Try the CountDownTimer. It's executed on the UI Thread and gives you information on exactly how much time passed since the last invocation. Adding an example to be more clear: Let's say you want to update the view after 300 ms ,150 ms and 75 ms that makes the total time 525ms and the minimum interval 50ms

so:

    private class TestCT extends CountDownTimer{

            private long mFirstTime;
            private long mTotalTime;
            private long mLastRecognizedEvent=0;

            public TestCT(long millisInFuture, long countDownInterval, long firstEventTime) {
                super(millisInFuture, countDownInterval);
                mTotalTime=millisInFuture;
                mFirstTime=firstEventTime;
            }

            @Override
            public void onTick(long millisUntilFinished) {
                long elapsed=mTotalTime-millisUntilFinished;
                elapsed-=mLastRecognizedEvent;
                if(elapsed>=mFirstTime){
                    updateTheView();
                    mLastRecognizedEvent+=mFirstTime;
                    mFirstTime=mFirstTime/2;
                }
            }

            @Override
            public void onFinish() {
                updateTheView();

            }


        }

new TestCT(525,50,300).start();

Not tested this so not 100% sure will work but it should give you an idea

Pasquale Anatriello
  • 2,355
  • 1
  • 16
  • 16
  • I've tried but in CountDownTimer the second argument [ ex.: new CountDownTimer(30000, 1000) ] is not variable, is costant. I need fixed time in second argument. – murdock Dec 10 '13 at 15:15
  • What do you mean with is costant? – Pasquale Anatriello Dec 10 '13 at 15:36
  • Ok. I did not explain myself clearly. I do not need fixed time!! This a sample scenario: I have a total amount of second(the first argument of CountDown). First event--> I change text color after 300ms; Second event--> I change text color after 150ms; Third event--> I change text color after 150ms.......... – murdock Dec 10 '13 at 16:01
  • That means countdown timer is perfect for you. just set the update time to the minimum time you'll ever need and do the math yourself based on the time to the timer completion that you get in your callback – Pasquale Anatriello Dec 10 '13 at 16:03
  • [link](http://developer.android.com/reference/android/os/CountDownTimer.html). Pasquale, sorry to bother you, can you show me an example? This link use a fixed arguments. I don't understand a callback mechanism. – murdock Dec 10 '13 at 16:44