M.D
M.D

Reputation: 101

Timer in Android not working

I'm trying to get an alert box to show up 5 seconds after my application starts on Android. But it's not working and I don't know why. Here is the code:

package com.example.helloandroid;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;



public class HelloAndroid extends Activity {

public void showalert() {
    /* alert box--------------------------- */
    AlertDialog dialog=new AlertDialog.Builder(HelloAndroid.this).create();
    dialog.setTitle("Test");
    dialog.setMessage("Test");
    dialog.setButton("Test 1",
    new DialogInterface.OnClickListener()
    {
    public void onClick(DialogInterface dialog, int whichButton)
    {
    /* Do some stuff */
    }
    });
    dialog.setButton2("Test 2",
    new DialogInterface.OnClickListener()
    {
    public void onClick(DialogInterface dialog, int whichButton)
    {
    /* Do some stuff */
    }
    });
    dialog.setButton3("Test 3",
    new DialogInterface.OnClickListener()
    {
    public void onClick(DialogInterface dialog, int whichButton)
    {
    /* Do some stuff */
    }
    });
    dialog.show();
    /* -------------------------------------- */
}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);


    new Timer().schedule(new TimerTask()
    { 
        public void run() 
        { 
            showalert();
        } 
    }, 5000);

    setContentView(R.layout.main);

}

}

Can anyone see what I am doing wrong? Why doesnt the alert show up...? Thanks for your help!

Best whishes, Michael

EDIT (This is the working code after some modifying):

package com.example.helloandroid;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;



public class HelloAndroid extends Activity {

    public void showalert() {
        /* alert box--------------------------- */
        AlertDialog dialog=new AlertDialog.Builder(HelloAndroid.this).create();
        dialog.setTitle("Test");
        dialog.setMessage("Test");
        dialog.setButton("Test 1",
        new DialogInterface.OnClickListener()
        {
        public void onClick(DialogInterface dialog, int whichButton)
        {
        /* Do some stuff */
        }
        });
        dialog.setButton2("Test 2",
        new DialogInterface.OnClickListener()
        {
        public void onClick(DialogInterface dialog, int whichButton)
        {
        /* Do some stuff */
        }
        });
        dialog.setButton3("Test 3",
        new DialogInterface.OnClickListener()
        {
        public void onClick(DialogInterface dialog, int whichButton)
        {
        /* Do some stuff */
        }
        });
        dialog.show();
        /* -------------------------------------- */
    }

    public final void timerAlert(){ 

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                showalert();
            }
        }, 5000);

    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        timerAlert();
    }
}

Upvotes: 0

Views: 9625

Answers (3)

Mikhaili
Mikhaili

Reputation: 161

First way

new Timer().schedule(new TimerTask()
{
@Override 
   public void run()
   {
   // your action hear
   }
}, timeToExecute);

this execute your action after timeToExecute seconds. It works like a charm and don't forget the @Override.

Second Way as you post in Edit with handle.postDelayed.

Upvotes: 0

GBouerat
GBouerat

Reputation: 480

You can also use postDelayed() method in Handler

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        showalert();
    }
}, 5000);

http://developer.android.com/reference/android/os/Handler.html#postDelayed%28java.lang.Runnable,%20long%29

Upvotes: 3

Curtain
Curtain

Reputation: 1972

Try this (have your showalert-function left):

    public final void timerAlert(){ 

      t = new Timer();
      tt = new TimerTask() {

          @Override //Override!!
          public void run() {
              showalert();
              t.purge();
          }

      };
      t.schedule(tt, 5000);

}

First, define your functions like above, then in onCreate you put the timerAlert();:

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

timerAlert();

}

Upvotes: 0

Related Questions