0

I have a activity called "OtpVerification" containing timer of 30 second and during this process if I exit from app and then reopen app it creates problem 1)after few seconds when I reopen app then app starts from MainActivity instead of that activity and 2) app crashes.

here is scenario:-Scenario: A 30 seconds timer starts when Automatic OTP verification process starts, now a user tries to end this process and exit the application by pressing back button, he then immediately relaunches the app within few seconds. Now as the timer expires and the application tries to launch the manual OTP verification screen, this creates a fatal exception and application gets crashed.

here is code:-

@SuppressLint("SetTextI18n")
private void init() {// initialize view controls
    //Initialize a new CountDownTimer instance
    long m_MillisInFuture = 30000;// timer value
    long m_CountDownInterval = 1000;// timer break up
    m_oTimer = new CountDownTimer(m_MillisInFuture, m_CountDownInterval) {
        public void onTick(long millisUntilFinished) {
            @SuppressWarnings("UnusedAssignment") long millis = millisUntilFinished;
            @SuppressLint("DefaultLocale") 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;
            m_timerProgress.setProgress(m_n_ProgressStatus);
        }

        public void onFinish() {// when timer finished
            /*This is new change ....check if app is in backround or foreground*/
            /*if app is in background*/
            if (NetworkUtil.isAppIsInBackground(getApplicationContext())) {
                System.out.println("In Background");
                /*if app is in background start service */
                Intent startService = new Intent(getApplicationContext(), OTPIntentService.class); // if condition match then start service to notify server regarding app installation
                getApplicationContext().startService(startService);// start service
            } else {
                System.out.println("In Foreground");
                /*if app is in forground than send request to server*/
                verifyOTP();
            }


        }
    }.start();// start timer
    // retreive progress bar count........
    int progressBarMaximumValue = (int) (m_MillisInFuture / m_CountDownInterval);
    //Set ProgressBar maximum value
    //ProgressBar range (0 to maximum value)
    m_timerProgress.setMax(progressBarMaximumValue);
    //Display the CountDownTimer initial value
    tv.setText(progressBarMaximumValue + "Seconds...");
}

@Override
public void onDestroy() {// unregister broadcast receiver ........
    super.onDestroy();
    getApplicationContext().unregisterReceiver(m_oOtpReceiver);// unregistaer broadcast receiver.

}
VipiN Negi
  • 2,490
  • 2
  • 16
  • 34
rahul
  • 171
  • 1
  • 2
  • 9

2 Answers2

0

try to unregister your Receiver in onPause() and create and change flag to false and in onResume() check if flag is false then reregister the receiver again. i had similar kind of problem in my App, these did help me.

Bhoomit_BB
  • 51
  • 2
  • send the logs if possible and also where exactly the error is occuring – Bhoomit_BB May 30 '16 at 11:26
  • FATAL EXCEPTION: main Process: com.example.devui1.rewardapp, PID: 16233 android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@427b5348 is not valid; is your activity running? – rahul May 30 '16 at 11:30
0

Kind of like this i implemented in my application, i called receiver from my service

@Override
protected void onResume() {
    super.onResume();
    registerReceiver();
}

@Override
protected void onPause() {
    LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver);
    isReceiverRegistered = false;
    super.onPause();
}

private void registerReceiver(){
    if(!isReceiverRegistered) {
        LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
                new IntentFilter(SessionStore.REGISTRATION_COMPLETE));
        isReceiverRegistered = true;
    }
}
Bhoomit_BB
  • 51
  • 2