Why my custom dialog can not close automatically?

I defined a CustomDialog class as follow :

public class CustomDialog {
    Dialog dialog;
    public void show(Contex contex, int duration){
        Handler timeHandler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
    switch (msg.what) {
    case 1:
    dialog.dismiss();
    break;
    default:
    break;
    }               
    };
       dialog = new dialog(contex);
       dialog.show();
       Message message = new Message();
   message.what = 1 ;
   timeHandler.sendMessageDelayed(message, duration);
    }
}

In the Activity , I new an CustomDialog object , and call the show() method , but the dialog can not close automatically , could anybody tell me why ? The code in the activity :

CustomDialog myDialog = new CustomDialog();
myDialog.show(this,2000);

It did not work.Is it a formal way to create a custom dialog ? If it's not , could anyone tell me what is the right way to create one ?

Answers


Try this:

    public class CustomDialog {
        Dialog dialog;
        public void show(Contex contex, int duration){
            Handler timeHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
        switch (msg.what) {
        case 1:
        dismiss();
        break;
        default:
        break;
        }               
        };
           dialog = new dialog(contex);
           dialog.show();Timer timer = new Timer();
Timer timer=new Timer(); 
   timer.schedule(new TimerTask() {
        @Override
        public void run() {
        //dismiss the dialog here
        dialog.dismiss();
        }
    }, duration);

        }
    }

Need Your Help

Does compilng java code to exe (e.g. using Launch4Java) ensure code cannot be reversed engineered?

java reverse-engineering launch4j

After experimenting myself, I am convinced that java code obsfucation is not safe in terms of preventing reverse code engineering.

open source library for Optical mark reader

java image-processing

I want a open source library for optical mark reading (OMR).Is there any such library?