i want my countdown timer continue running if i close the app through the back button or close app

namespace countdowntimer
{
[Activity(Label = "countdowntimer", MainLauncher = true)]
public class MainActivity : Activity, View.IOnClickListener
{
public long timeCountInMilliSeconds = 1 * 60000;
private enum TimerStatus
{
STARTED,
STOPPED
}

    private TimerStatus timerStatus = TimerStatus.STOPPED;
    public ProgressBar progressBarCircle;
    public EditText editTextMinute;
    public TextView textViewTime;
    private ImageView imageViewReset;
    public ImageView imageViewStartStop;
    public CountDown countDownTimer;
    public CountDownTimer countdown2;
    public long mStartTimeInMillis;
    public long mTimeLeftInMillis;
    public long mEndTime;

    public event TickEvent Tick;
    public event FinishEvent Finish;


    protected override void OnCreate(Bundle savedInstanceState)
    {

        base.OnCreate(savedInstanceState);

        var intent = new Intent(this, typeof(myService));
        StartService(intent);

        SetContentView(Resource.Layout.Main);


        initViews();
        initListeners();


    }
    public void initViews()
    {

        editTextMinute = FindViewById<EditText>(Resource.Id.editTextMinute);
        textViewTime = FindViewById<TextView>(Resource.Id.textViewTime);
        imageViewReset = FindViewById<ImageView>(Resource.Id.imageViewReset);
        imageViewStartStop = FindViewById<ImageView>(Resource.Id.imageViewStartStop);
        progressBarCircle = FindViewById<ProgressBar>(Resource.Id.progressBarCircle);

    }
    public void initListeners()
    {
        imageViewReset.SetOnClickListener(this);
        imageViewStartStop.SetOnClickListener(this);


    }

    public void OnClick(View v)
    {
        switch (v.Id)
        {
            case Resource.Id.imageViewReset:
                reset();
                break;
            case Resource.Id.imageViewStartStop:
                startStop();
                break;
            default:
                break;
        }
    }
    private void reset()
    {
        stopCountDownTimer();
        startCountDownTimer();

    }
    public void startStop()
    {
        try
        {
            if (timerStatus == TimerStatus.STOPPED)
            {
                setTimerValues();
                setProgressBarValues();

                imageViewReset.Visibility = ViewStates.Visible;
                imageViewStartStop.SetImageResource(Resource.Drawable.icon_stop);

                editTextMinute.Enabled = false;

                timerStatus = TimerStatus.STARTED;
                startCountDownTimer();
            }

            else
            {
                imageViewReset.Visibility = ViewStates.Gone;
                imageViewStartStop.SetImageResource(Resource.Drawable.icon_start);
                editTextMinute.Enabled = true;
                timerStatus = TimerStatus.STOPPED;

                stopCountDownTimer();
            }
        }
        catch (Exception)
        {


        }

    }

    public void setTimerValues()
    {



        int time = 0;
        try
        {


            if (editTextMinute.Text != "")
            {
                time = int.Parse(editTextMinute.Text.ToString().Trim());
            }
            else
            {
                Toast.MakeText(Application.Context, Resource.String.message_minutes, ToastLength.Long).Show();
            }
        }
        catch (Exception)
        {


        }

        timeCountInMilliSeconds = time * 60 * 1000;

    }

    public void startCountDownTimer()
    {

        countDownTimer = new CountDown(timeCountInMilliSeconds, 1000);

        countDownTimer.Tick += CountDownTimer_Tick;

        countDownTimer.Finish += CountDownTimer_Finish;


        countDownTimer.Start();

    }

    private void CountDownTimer_Finish()
    {
        try
        {
            if (editTextMinute.Text != "")
            {
                textViewTime.Text = hmsTimeFormatter(timeCountInMilliSeconds);

                setProgressBarValues();                   
                editTextMinute.Enabled = true;
                timerStatus = TimerStatus.STOPPED;
                imageViewStartStop.SetImageResource(Resource.Drawable.icon_start);
                Toast.MakeText(Application.Context, "Time out", ToastLength.Long).Show();
                CloseApp();

            }
            else
            {
                editTextMinute.Enabled = true;
                imageViewReset.Visibility = ViewStates.Gone;
                imageViewStartStop.SetImageResource(Resource.Drawable.icon_start);
                timerStatus = TimerStatus.STOPPED;
            }

        }
        catch (Exception)
        {


        }

    }

    public void CountDownTimer_Tick(long millisUntilFinished)
    {
        try
        {
            textViewTime.Text = hmsTimeFormatter(millisUntilFinished);
            progressBarCircle.Progress = Convert.ToInt32(millisUntilFinished / 1000); 
        }
        catch (Exception)
        {


        }




    }

    public string hmsTimeFormatter(long milliSeconds)

    {
        string hms = string.Format("{0:D2}:{1:D2}:{2:D2}",
            TimeUnit.Milliseconds.ToHours(milliSeconds),
            TimeUnit.Milliseconds.ToMinutes(milliSeconds) - TimeUnit.Hours.ToMinutes(TimeUnit.Milliseconds.ToHours(milliSeconds)),
            TimeUnit.Milliseconds.ToSeconds(milliSeconds) - TimeUnit.Minutes.ToSeconds(TimeUnit.Milliseconds.ToMinutes(milliSeconds)));

        return hms;
    }


    private void stopCountDownTimer()
    {

        countDownTimer.Cancel();

    }
    private void setProgressBarValues()
    {
        try
        {
            progressBarCircle.Max = Convert.ToInt32(timeCountInMilliSeconds / 1000);
            progressBarCircle.Progress = Convert.ToInt32(timeCountInMilliSeconds / 1000);
        }
        catch (Exception)
        {


        }




    }

    public void CloseApp()
    {
        Java.Lang.JavaSystem.Exit(0);
        // Android.OS.Process.KillProcess(Android.OS.Process.MyPid());

    }


}

}

2- countdown class

namespace countdowntimer
{
public delegate void TickEvent(long millisUntilFinished);
public delegate void FinishEvent();

public   class CountDown : CountDownTimer
{

    public event TickEvent Tick;
    public event FinishEvent Finish;



    public  CountDown(long totaltime, long interval)
        : base(totaltime, interval)
    {
    }

    public  override void OnTick(long millisUntilFinished)
    {

        if (Tick != null)
            Tick(millisUntilFinished);
    }

    public override void OnFinish()
    {
        if (Finish != null)
            Finish();
    }

}

}

Sign In or Register to comment.