Power Man
Power Man

Reputation: 166

android: with delay in thread

i'm new in android. i made an app with service and thread to show a Toast every 5 seconds:

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service {
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    Handler mHandler = new Handler();

    @Override
    public void onCreate() {
        super.onCreate();

        final Runnable RunnableUpdateResults = new Runnable() {
            public void run() {
                Toast.makeText(getBaseContext(), "Hello", Toast.LENGTH_SHORT).show();
            }
        };
        new Thread() {
            public void run() {
                try {                   
                        mHandler.postDelayed(RunnableUpdateResults);
                        sleep(5000);
               } catch (InterruptedException e) {e.printStackTrace();}
            }
        }.start();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) 
    {
         return START_STICKY;
    }

    public void onStart(final Context context,Intent intent, int startId)
    {
    }
}

but my Toast shown only once. with no crash. i used Handler's postDelayed function for this purpose. It will run your code with specified delay on the main UI thread, so you will be able to update UI controls but it dose not work too.

any solution to do a task repetitively ?

Upvotes: 0

Views: 1167

Answers (2)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132972

call Thread.sleep inside while loop as:

boolean isThreadRunning=true;
 new Thread() {
            public void run() {
                try {    
                        while(isThreadRunning){               
                        mHandler.postDelayed(RunnableUpdateResults);
                        sleep(5000);
                       }
               } catch (InterruptedException e) {e.printStackTrace();}
            }
        }.start();

To stop Thread make isThreadRunning=false;

You can achieve same using handler.postDelayed

mHandler.postDelayed(RunnableUpdateResults,5000);

and in RunnableUpdateResults call mHandler.postDelayed :

final Runnable RunnableUpdateResults = new Runnable() {
            public void run() {
                mHandler.postDelayed(RunnableUpdateResults,5000);
                Toast.makeText(getBaseContext(), 
                         "Hello", Toast.LENGTH_SHORT).show();
            }
        };

To stop Handler call removeCallbacks method:

mHandler.removeCallbacks(RunnableUpdateResults);

Upvotes: 1

Ion Aalbers
Ion Aalbers

Reputation: 8030

Use a Timer with a TimerTask

    new Timer().schedule(new TimerTask() {
        @Override
        public void run() {
            mHandler.post(RunnableUpdateResults);                
        }
    }, 0, 5000);

Upvotes: 0

Related Questions