我在点击按钮时加载方法,但它没有花费我给perfrom action的时间。任何人都告诉我我做错了什么。
这是点击按钮: -
resand_otp_new.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (VerificationCount < 2) {
if(br != null)
{
br.show();
}
checkotp.setText("");
HashMap<String, String> formData = new HashMap<>();
formData.put("Phone", Phone);
webServiceHandler.requestToServer((getResources().getString(R.string.api_end_point)) + "SendOTPForgetPassword", WebService.ADDLIST, formData, true);
VerificationCount++;
timer.start();
mTvCountDown.setText("00:00:30");
resand_otp_new.setVisibility(View.INVISIBLE);
verifyResendOtp();
} else {
Toast.makeText(v.getContext(), "You Have Used Maximum Chances of OTP !", Toast.LENGTH_SHORT).show();
}
}
});
我加载方法verifyResendOtp()30秒。但它不需要时间立即执行。
这是我的方法: -
private void verifyResendOtp() {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (generalUtilities.isConnected()) {
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(IncominMsg.KEY_PREFERNCE, MODE_PRIVATE);
otpnumber = sharedPreferences.getString(IncominMsg.KEY_OTP, "");
Log.e("otpnumberverifiy....", otpnumber);
checkotp.setText(otpnumber);
HashMap<String, String> formData = new HashMap<>();
formData.put("Phone", Phone);
formData.put("OTP", checkotp.getText().toString().trim());
webServiceHandler.requestToServer((getResources().getString(R.string.api_end_point)) + "CheckOTPForgetPassword", WebService.FORGET_PASSWORD, formData, true);
} else {
generalUtilities.showAlertDialog("HEY USER", "Please Connect To the Internet", getApplicationContext());
}
}
}, 30000);
}
答案 0 :(得分:0)
我认为这里存在设计错误,而不是在方法中实现Handler,为什么不在onClickListener中创建一个Handler并使实现成为runnable并从那里调用方法。
如下所示
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
verifyResendOtp();
}
}, 30000);
你的重构方法
private void verifyResendOtp() {
if (generalUtilities.isConnected()) {
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(IncominMsg.KEY_PREFERNCE, MODE_PRIVATE);
otpnumber = sharedPreferences.getString(IncominMsg.KEY_OTP, "");
Log.e("otpnumberverifiy....", otpnumber);
checkotp.setText(otpnumber);
HashMap<String, String> formData = new HashMap<>();
formData.put("Phone", Phone);
formData.put("OTP", checkotp.getText().toString().trim());
webServiceHandler.requestToServer((getResources().getString(R.string.api_end_point)) + "CheckOTPForgetPassword", WebService.FORGET_PASSWORD, formData, true);
} else {
generalUtilities.showAlertDialog("HEY USER", "Please Connect To the Internet", getApplicationContext());
}
}