Alert Dialog is visible until the startVibrate() method completes its task but I want to dismiss it as soon as clicked on Ok
AlertDialog.Builder builder = new Builder(VibrationActivity.this);
builder.setCancelable(false);
builder.setTitle("Vibration Test");
builder.setMessage("Your device will vibrate now.");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss(); //Dismiss the dialog
startVibration(); //Start the test
}
});
builder.show();
public void startVibration(){
for (int i = 1; i <= 5; i++) {
try {
vibrator.vibrate(500); // Vibration time in milliseconds
Thread.sleep(750); // Interval between two vibration
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
You should just launch your AlertDialog within your Activity. your Thread should dispatch a message via a handler to the Activity to know when to show and dismiss your Dialog.
首先调用startVibrate方法,然后关闭对话框,最好调用cancel而不是dismiss。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.