当Broadcastreceiver在android中读取OTP时,如何用倒数计时器关闭进度条

时间:2016-04-26 03:25:45

标签: android

我正在开发一个应用程序,其中包含读取服务器发送的OTP的广播接收器,还包含一个带有倒数计时器的进度条。我要做的就是在广播接收器收到otp后立即关闭进度条将otp发送到服务器。我该怎么做才能帮忙。

这是广播接收器的代码: -

public class CSmsBroadcastReceiver extends BroadcastReceiver {
private static final String s_szTAG = CSmsBroadcastReceiver.class.getSimpleName();
private static String m_szOtpCode;

@Override
public void onReceive(Context context, Intent intent) {
    final Bundle bundle = intent.getExtras();
    try {
        if (bundle != null) {
            Object[] pdusObj = (Object[]) bundle.get("pdus");
            assert pdusObj != null;
            for (Object aPdusObj : pdusObj) {
                @SuppressWarnings("deprecation") SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) aPdusObj);
                String phoneNumber = currentMessage.getDisplayOriginatingAddress();
                String message = currentMessage.getDisplayMessageBody();

                Log.e(s_szTAG, "Received SMS: " + message + ", Sender: " + phoneNumber);

                // checking sms sender address....
                if (phoneNumber.toLowerCase().contains("+91997159001".toLowerCase())) {
                    // verification code from sms
                    m_szOtpCode = getVerificationCode(message);
                    assert m_szOtpCode != null;
                    String input = m_szOtpCode.trim();

                    Log.e(s_szTAG, "OTP received: " + m_szOtpCode);
                    COTPVerificationDataStorage.getInstance().setM_szOtp(input);// getting otp from SMS and set to otpverificationstorage class
                } else {
                    return;
                }

            }
        }
    } catch (Exception e) {
        Log.e(s_szTAG, "Exception: " + e.getMessage());
    }
}

/**
 * Getting the OTP from sms message body
 * ':' is the separator of OTP from the message
 *
 * @param message
 * @return
 */
@SuppressWarnings("JavaDoc")
private String getVerificationCode(String message) {
    String code;
    int index = message.indexOf(":");

    if (index != -1) {
        int start = index + 2;
        int length = 6;
        code = message.substring(start, start + length);
        return code;
    }
    COTPVerificationDataStorage.getInstance().setM_szOtp(m_szOtpCode);
    return null;
}

}

以下是progressbare存在的活动代码: -

private void init() {
    pb = (ProgressBar) m_Main.findViewById(R.id.pb);
    final TextView tv = (TextView) m_Main.findViewById(R.id.tv);
    @SuppressWarnings("UnusedAssignment") final TextView validationText = (TextView) m_Main.findViewById(R.id.validatingmessage);
    tv.setText("00:00");
    //Initialize a new CountDownTimer instance
    long m_MillisInFuture = 30000;
    long m_CountDownInterval = 1000;
    new CountDownTimer(m_MillisInFuture, m_CountDownInterval) {
        public void onTick(long millisUntilFinished) {
            @SuppressWarnings("UnusedAssignment") long millis = millisUntilFinished;
            String hms = String.format("%02d:%02d", TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
                    TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
                            TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)));
            System.out.println(hms);
            tv.setText(hms);
            //Another one second passed
            //Each second ProgressBar progress counter added one
            m_n_ProgressStatus += 1;
            pb.setProgress(m_n_ProgressStatus);
        }

        public void onFinish() {
            // retreive user data from shared preferencce........
            HashMap<String, String> user = m_oSessionManagement.getRegistrationDetails();
            m_szEncryptedPassword = user.get(CRegistrationSessionManagement.s_szKEY_PASSWORD).trim();
            m_szMobileNumber = user.get(CRegistrationSessionManagement.s_szKEY_MOBILENUMBER).trim();
            // exc=ecuting request for otp verfiifcation to server
            new COtpVerify().execute();
        }
    }.start();
    // retreive progress bar count........
    int progressBarMaximumValue = (int) (m_MillisInFuture / m_CountDownInterval);
    //Set ProgressBar maximum value
    //ProgressBar range (0 to maximum value)
    pb.setMax(progressBarMaximumValue);
    //Display the CountDownTimer initial value
    tv.setText(progressBarMaximumValue + "Seconds...");
}

0 个答案:

没有答案