简体   繁体   English

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

[英]How to cancel countdown timer with progress bar in android?

I am developing an app which contain CountdownTimer with progress bar ,scenario is something like that there is a BroadcastReceiver which read sms from server .What I want to dismiss progress bar as soon as broadcast read sms and execute url .How can I do that kindly help me. 我正在开发一个包含带进度条的CountdownTimer的应用程序,场景类似于有一个BroadcastReceiver从服务器读取短信。我想在广播读取短信和执行网址时解除进度条。我怎么能这样做帮我。 its a week I am searching . 我正在寻找一周。

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;

}

and here is my countdown timer:- 这是我的倒数计时器: -

@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...");
}

What you can do is 你能做的是

  1. create class level variable for timer object 为计时器对象创建类级别变量
  2. Then create dynamic broadcast in Activity/Fragment 然后在Activity / Fragment中创建动态广播
  3. Then call timer.cancel(); 然后调用timer.cancel(); in your onReceive() 在你的onReceive()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM