0

I have created a periodic execution of runnable task by using handler in android, but I want to pass an argument in that periodic task execution, I have tried 2 approaches to pass an argument,

1 - By declaring a class in the method

void Foo(String str) {
    class OneShotTask implements Runnable {
        String str;
        OneShotTask(String s) { str = s; }
        public void run() {
            someFunc(str);
        }
    }
    Thread t = new Thread(new OneShotTask(str));
    t.start();
}

2 - and by putting it in a function

String paramStr = "a parameter";
Runnable myRunnable = createRunnable(paramStr);

private Runnable createRunnable(final String paramStr){

    Runnable aRunnable = new Runnable(){
        public void run(){
            someFunc(paramStr);
        }
    };

    return aRunnable;

}

Reference for these above 2 approach can be found on Runnable with a parameter?

below is my code in which I have used the 1 approach, but either I use 1 or 2 approach there are two problems that remains by passing an argument in a periodic runnable task,

1 problem - the counter variable inside my class is overwritten every time the code repeats itself. If I define this counter variable outside of class and method as a global variable instead of a local variable then it works fine, but that is not my requirement.

2 problem - assume 1 problem is solved by using the counter variable as a global variable then the counter increments after the first execution and with the second execution it unregistered the sensor i.e. mSensorListener but it is unable to remove the further Callbacks with this command,

// Removes pending code execution
staticDetectionHandler.removeCallbacks(staticDetectionRunnableCode(null));

this above command is also not working with the onPause method, it still periodically execute after this command execution ? I don't able to understand what is happening, can anyone provide a solution for this ? below is my code,

Runnable staticDetectionRunnableCode(String str){

    class staticDetectionRunnableClass implements Runnable{
        String str;
        private int counter = 0;

        staticDetectionRunnableClass(String str){
            this.str = str;
        }

        @Override
        public void run() {
            // Do something here
            Log.e("", "staticDetectinoHandler Called");

            Debug.out(str + " and value of " + counter);

            // Repeat this runnable code block again every 5 sec, hence periodic execution...
            staticDetectionHandler.postDelayed(staticDetectionRunnableCode(str), constants.delay_in_msec * 5);  // for 5 second

            if(counter >= 1){
                // Removes pending code execution
                staticDetectionHandler.removeCallbacks(staticDetectionRunnableCode(null));

                // unregister listener
                mSensorManager.unregisterListener(mSensorListener);
            }
            counter++;
        }
    }

    Thread t = new Thread(new staticDetectionRunnableClass(str));
    return t;
}
Community
  • 1
  • 1
  • the first approach is ok, i dont see what problems you have with it though – pskink Jun 04 '15 at 16:19
  • @pskink the problem is by passing an argument with this above periodic task by using the 1 approach, that is the above 2 problems I am facing right now ! – Shehzad Jalal Jun 04 '15 at 16:32
  • sorry but i dont see any problem – pskink Jun 04 '15 at 16:33
  • @pskink you run this above code by making a handler, you will sure able to see – Shehzad Jalal Jun 04 '15 at 16:36
  • i made many times classes that implements Runnable and it worked like a charm with a Handler – pskink Jun 04 '15 at 16:47
  • If I understand the question right, he is having a problem removing the callback. I am little confused about the counter logic. Won't it be always 1 second time around and if you really don't want to run this at all second time around, why do you even do postDelayed? Can you post the counter value that you see? I think your task executes again because you are doing postDelayed before removeCallbacks. Why not put the postdelayed inside else condition? – Raghu Jun 04 '15 at 17:53
  • @Raghu thanks man, you have solved my 2 problem, I have putted it into the else part and now it is able to remove the Callbacks, but still I have to take the counter variable as a global variable. If I use counter variable locally its value is always zero, means its overwriting the value of counter, I am always see the counter value as counter = 0 the logic is that, I have use the postDelayed to post a delay of 5 second for every periodic execution, so after first execution the counter value should increment by 1 so for the next execution it will unregistered the sensor and remove callBacks – Shehzad Jalal Jun 05 '15 at 03:06

0 Answers0