user3045529
user3045529

Reputation: 37

Android how call function after amount of time in loop

I am new in Android programming, I used to program microcontroller, now I need a few assistance from you guys. I want to call two function with delay after each function called repeatedly until the stop button pressed.

btnStart.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //I want perform these series of function repeatedly
                //until stop button pressed
                while(true){
                    bluetoothDisconnect();
                    delay(3000);
                    bluetoothConnect();
                    delay(3000);
                    if(status == true){
                        break;
                    }
                }
            }

        });

        btnStop.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                status = true;

            }

        });

Big Thanks,

Upvotes: 1

Views: 2618

Answers (3)

phil_g
phil_g

Reputation: 516

Try this:

  btnStart.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            status = false;

            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    doLoop();
                }
            });

            t.start();
        }

    });

    btnStop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
             status = true;
        }
    });

where

 private void doLoop(){
    do {
        bluetoothDisconnect();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        bluetoothConnect();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }   while (!status);
}

and status is a global variable, FALSE by default (declared as boolean status = false) This code works as a simple solution, however I'd suggest you to look at the AsyncTask class: http://developer.android.com/guide/components/processes-and-threads.html

Upvotes: 0

Smogger
Smogger

Reputation: 553

Create an handler to delay the task .

Create the runnable thread and run your handler like:

Handler mhandler=new Handler();

Runnable  mRunnable=new Runnable() {

                @Override
                public void run() {
                    bluetoothDisconnect(); 
             btnStart.performClick() ; 
                }

    };



btnStart.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //I want perform these series of function repeatedly
            //until stop button pressed
            while(true){

                bluetoothconnect();
                mhandler.postDelayed(mRunnable,3*1000);
                if(status == true){
                    break;
                }
            }
        }

    });

Upvotes: 0

You need to thread the code in btnStart otherwise it will lock the GUI. Sorry to not be more helpful, I write Android apps using Mono (C#), I am unsure how to do it in Java.

Upvotes: 1

Related Questions