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.