我有一个应用程序,当用户点击卡片时拨打相应的号码。所以我想添加延迟和取消功能,其中向用户显示警告对话框或进度条5秒钟,并使用取消按钮终止时间范围内的延迟。
我尝试使用处理程序和runnable但是无论何时启动调用,取消按钮都会关闭对话框窗口,但不会停止延迟。
还有其他方法吗?
感谢。
以下是我的 onClick 方法
@Override
public void onClick(final View view) {
if (getAdapterPosition() == 0) {
//Do nothing here - Header Position
} else if (getAdapterPosition() == 1) {
dialog = new ProgressDialog(view.getContext());
dialog.setCancelable(true);
//dialog.setIcon(resId);
dialog.setTitle("Calling Fire Service" );
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setIndeterminate(true);
dialog.setIndeterminateDrawable(view.getContext().getResources().getDrawable(R.drawable.police_animation));
dialog.show();
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:192"));
view.getContext().startActivity(callIntent);
}
}, 5000);
dialog.dismiss();
}
});
} else if (getAdapterPosition() == 2) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:193"));
view.getContext().startActivity(callIntent);
} else if (getAdapterPosition() == 3) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:191"));
view.getContext().startActivity(callIntent);
} else if (getAdapterPosition() == 4) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:18555"));
view.getContext().startActivity(callIntent);
} else if (getAdapterPosition() == 5) {
view.getContext().startActivity(new Intent(view.getContext(), SafetyActivity.class));
} else if (getAdapterPosition() == 6) {
view.getContext().startActivity(new Intent(view.getContext(), FirstAidActivity.class));
} else {
return;
}
}