tags:

views:

273

answers:

2

Hello,

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();
    }
}
A: 

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();

}
Julian Assange
Hello Julian,Thank you for the answer! But I still can't make the alert box appear. I had to make one change to your code example.I changed this: t = new Timer(); tt = new TimerTask() {To this: final Timer t = new Timer(); TimerTask tt = new TimerTask()And then I tried to run the app but no alert box...
Michael D
A: 

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

GBouerat
Hello GBouerat,Thanks for your reply! I tried your suggestion but I couldn't get it to work. I had to remove the @Override in your example because Eclipse was complaining about the run() function otherwise.
Michael D
Well now I sort of mixed your and Julian's code example together somehow and got it to work :D Thanks alot! I will try to edit my previous post so you can see the complete working code, if anyone else got the same problem.
Michael D