如何取消Android中的进度条倒数计时器?

时间:2016-04-26 05:16:43

标签: android countdowntimer android-broadcastreceiver

我正在开发一个包含CountdownTimer进度条的应用程序,方案类似于有BroadcastReceiver从服务器读取短信。我想在广播读取后立即关闭进度条短信和执行网址。我该怎么做才能帮助我。我正在寻找一周。

public BroadcastReceiver br = new BroadcastReceiver() {
    @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("+919971599909".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());
        }
    }

    @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;
    }
};
private IntentFilter inf;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    m_Main = inflater.inflate(R.layout.otp_auto_verified, container, false);

    inf = new IntentFilter();
    inf.addAction("android.provider.Telephony.SMS_RECEIVED");
    getActivity().registerReceiver(br, inf);

    getUserDetails();// getuser deatails....
    init();// initialize controls...
    return m_Main;

}

这是我的倒数计时器: -

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    m_Main = inflater.inflate(R.layout.otp_auto_verified, container, false);
    getUserDetails();// getuser deatails....
    init();// initialize controls...
    return m_Main;

}

private void getUserDetails() {
    m_oSessionManagement = new CRegistrationSessionManagement(getActivity());
}

private void init() {
    pb = (ProgressBar) m_Main.findViewById(R.id.pb);
    m_ProgressView = (CircularProgressView) m_Main.findViewById(R.id.progress_view);
    m_TextView = (TextView) m_Main.findViewById(R.id.toolbar_title);
    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;
    timer = 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...");
}

1 个答案:

答案 0 :(得分:0)

你能做的是

  1. 为计时器对象创建类级别变量
  2. 然后在活动/片段
  3. 中创建动态广播
  4. 然后在timer.cancel();
  5. 中致电onReceive()