0

I have the following class:

public class TimerTaskHelper {

    private static TimerTaskHelper instance = null;
    private static Integer ONE_MINUTE = 60000;
    private static Handler handler;
    private static Runnable runnableInstance;
    private static Container container;

    public static TimerTaskHelper getInstance(Container c) {
        if (instance == null) {
            instance = new TimerTaskHelper(c);
        }
        return instance;
    }

    public TimerTaskHelper(Container c) {
        this.handler =  new Handler();
        this.runnableInstance = runnableCode;
        this.container = c;
        // Kick off the first runnableInstance task right away
        handler.post(runnableCode);
    }

    // Define the task to be run here
    private Runnable runnableCode = new Runnable() {
        @Override
        public void run() {
            // Do something here on the main thread
            Logger.d("Called");
            // Repeat this runnableInstance code again every 2 seconds
            handler.postDelayed(runnableCode, 2000);
            container.notifyObservers();
        }
    };

    public void stopExecution() {
        handler.removeCallbacks(runnableInstance);
        instance = null;
    }

}

I'm able to get instance from controller using the:

mTimerTaskHelper = TimerTaskHelper.getInstance(container);

But i would like to get callback in controller after the each

   private Runnable runnableCode = new Runnable() {
        @Override
        public void run() {

and

public void stopExecution() {
        handler.removeCallbacks(runnableInstance);
        instance = null;
    }

From controller.

How can i do it in the best way please?

1
  • First thing: what is the point of making your attributes ... static?! Commented Oct 20, 2015 at 11:00

1 Answer 1

3

I think you want to something like this.

interface CallBack {
void callBackMethod();
}

public class TimerTaskHelper {

private static TimerTaskHelper instance = null;
private static Integer ONE_MINUTE = 60000;
private static Handler handler;
private static Runnable runnableInstance;
private static Container container;
private CallBack callback;
public void setCallBack(CallBack cb){
 callback=cb;
}
public static TimerTaskHelper getInstance(Container c) {
    if (instance == null) {
        instance = new TimerTaskHelper(c);
    }
    return instance;
}

public TimerTaskHelper(Container c) {
    this.handler =  new Handler();
    this.runnableInstance = runnableCode;
    this.container = c;
    // Kick off the first runnableInstance task right away
    handler.post(runnableCode);
}

// Define the task to be run here
private Runnable runnableCode = new Runnable() {
    @Override
    public void run() {
        // Do something here on the main thread
        Logger.d("Called");
        // Repeat this runnableInstance code again every 2 seconds
        handler.postDelayed(runnableCode, 2000);
        container.notifyObservers();
        if(callback!=null){
           callback.callBackMethod();
    }
};
  mTimerTaskHelper = TimerTaskHelper.getInstance(container);
  mTimerTaskHelper.setCallBack(new CallBack(){
  void callBackMethod(){
  //TODO your code
  }});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.