在 startVibrate() 方法完成其任务之前,警报对话框是可见的,但我想在单击确定后立即将其关闭
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();
}
}
}