Gabriel Llamas
Gabriel Llamas

Reputation: 18437

Timer with callback

Info

Objective

public void do (){
 timer.scheduleAtFixedRate (new TimerTask (){
  public void run (){
   storedInterface.A ();
  }
 }, 0, speed);
}
private void startCallback (){
 runOnUiThread (new Runnable (){
  public void run (){
   storedInterface.A ();
  }
 });
}

public void do (){
 timer.scheduleAtFixedRate (new TimerTask (){
  public void run (){
   startCallback ();
  }
 }, 0, speed);
}

Posible workaround

Extend SensorClass from an Activity but SensorClass is not an Activity with methods onCreate(), onPause(), etc!!! I don't like this solution.


My question is: How can I call runOnUIThread() within a class that only recieves a context from an Activity? Or... Is there any other solution for my problem?

Thanks.

Upvotes: 4

Views: 6061

Answers (1)

Gabriel Llamas
Gabriel Llamas

Reputation: 18437

Solved using handler. Great tool!

public void do (){
    final Handler handler = new Handler ();
    timer.scheduleAtFixedRate (new TimerTask (){
        public void run (){
            handler.post (new Runnable (){
                public void run (){
                    storedInterface.A ();
                }
            });
        }
    }, 0, speed);
}

Upvotes: 9

Related Questions