0
private Thread thread;

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.e(TAG, "main OnCreate");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    d = new DBHandler(this);
    currentDate = Calendar.getInstance();
    formatter= new SimpleDateFormat("yyyy-MM-dd");


    thread =  new Thread(){
        @Override
        public void run(){
            try {
                synchronized(this){
                    wait(120000);
                    Log.e(TAG, "thread");
                }
            }
            catch(InterruptedException ex){
                Log.e(TAG, "error thread");
            }

            // TODO
        }
    };
    thread.start();


    if(d.getLastUpdateTime()!=formatter.format(currentDate.getTime())){
    //Put up the Yes/No message box
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder
            .setTitle("Must be updated..")
            .setMessage("Update?")
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    new UpdateDB(MainActivity.this);
                    txtLastUpdateShow.setText(d.getLastUpdateTime());
                    //  Toast.makeText(YesNoSampleActivity.this, "Yes button pressed",
                    //   Toast.LENGTH_SHORT).show();
                }
            })
            .setNegativeButton("No", null)                        //Do nothing on no
            .show();
}//if sonu

This is my main class on carreate. When started, it must wait 12 seconds in thread and then ask the user in dialog box. But in android studio with real device tablet, as soon as the application comes to screen, it asks. Why?

I made synchronized but still same.

EDIT

according to answers, i changed to handler method. Because i did not want to use a method to call this dialog, all declarations made in oncreate because.

final Handler handler = new Handler(); // this handler will run your code
    final Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (d.getLastUpdateTime() != formatter.format(currentDate.getTime())) {
                //Put up the Yes/No message box
                AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
                builder
                        .setTitle("db update...")
                        .setMessage("db?")
                        .setIcon(android.R.drawable.ic_dialog_alert)
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                new UpdateDB(MainActivity.this);
                                txtLastUpdateShow.setText(d.getLastUpdateTime());
                                //  Toast.makeText(YesNoSampleActivity.this, "Yes button pressed",
                                //   Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setNegativeButton("No", null)                        //Do nothing on no
                        .show();
            }//if sonu };
        }

    };
        handler.postDelayed(runnable,3000); // here you define a delay

error :

 java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activi

builder

this line.

1 Answers1

1

Your code doesn't work as expected, because the AlertDialog is not shown from the Thread you are trying to create. What is happening is: you create and start a Thread, it runs and waits for some amount of time. Meanwhile, in different thread your dialog is built and displayed. That's the thing about threads.

To fix your code, I suggest you create a Handler instance and use its postDelayed method, a Runnable instance that will show your dialog, and start your Runnable with a delay in your Handler:

Handler handler = new Handler(); // this handler will run your code
Runnable runnable = new Runnable() { /* your dialog shows here */ };
handler.postDelayed(runnable, 12000); // here you define a delay

After that in 12 seconds your code will be executed. Hope this helps.

EDIT

Here you may look at another approach that handles some of the limitations.

Community
  • 1
  • 1
Yury Fedorov
  • 14,508
  • 6
  • 50
  • 66
  • You cannot just copy your code into the new method, there may be issues with variable visibility and other problems. Make sure all the data you need is accessible from a new method – Yury Fedorov Dec 20 '15 at 14:07