Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends, i am showing a toast every 5 seconds when my app starting,then i show an toast every 2 seconds when click button but there are two toast running when i am press the button.i want on show only one toast every two seconds after button click.

Java
public class MainActivity extends ActionBarActivity {
    Button panic;
    String TimeHand="s";
    Timer timer;
    final Handler handler = new Handler();
    TimerTask timerTask;
    int k=5,l=2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        panic=(Button)findViewById(R.id.button);
        panic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (panic.getText().equals("Panic")) {
                    panic.setText("Disable Panic");
                    Toast.makeText(getApplicationContext(), "2 Seconds", Toast.LENGTH_LONG).show();

                    timer.cancel();
                    startTimer();
                    initializeTimerTask();
                    timer.schedule(timerTask,0,l*1000);

                }
                else if (panic.getText()=="Disable Panic")
                {
                    panic.setText("Panic");
                    Toast.makeText(getApplicationContext(), "60 Seconds", Toast.LENGTH_LONG).show();


                    timer.schedule(timerTask,0, k*1000);
                }
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();

        //onResume we start our timer so it can start when the app comes from the background
        startTimer();
    }

    public void startTimer() {
        //set a new Timer
        timer = new Timer();

        //initialize the TimerTask's job
        initializeTimerTask();

        timer.schedule(timerTask, 0, k * 1000); //
    }

    public void stoptimertask(View v) {
        //stop the timer, if it's not already null
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }

    public void reScheduleTimer(int t) {
        timer = new Timer("alertTimer",true);
        timerTask = new MyTimerTask();
        timer.schedule(timerTask, 0, t * 1000);
    }

    private class MyTimerTask extends TimerTask {
        @Override
        public void run() {

        }
    }
    public void initializeTimerTask() {

        timerTask = new TimerTask() {
            public void run() {

                //use a handler to run a toast that shows the current timestamp
                handler.post(new Runnable() {
                    public void run() {
                        //get the current timeStamp
                        Calendar calendar = Calendar.getInstance();
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd:MMMM:yyyy HH:mm:ss a");
                        final String strDate = simpleDateFormat.format(calendar.getTime());

                        //show the toast
                        int duration = Toast.LENGTH_SHORT;
                        Toast toast = Toast.makeText(getApplicationContext(), strDate, duration);
                        toast.show();
                    }
                });
            }
        };
    }


}
Posted
Updated 2-Jun-15 21:52pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900