title: I want my timer to run in background but it stops as soon it goes in background android
body: <p><strong>## This main activity class ##</strong></p>
  
  <p><strong>> Suggest me with suitable answer</strong></p>
  
  <pre><code>pref = getPreferences(MODE_PRIVATE);
              time = pref.getInt("key1", 300);
              if (time == 300) {                
  </code></pre>
  
  <p><strong>**getting initial values from shred preferances**</strong></p>
  
  <pre><code>                number.setText("01:00:00");
                  startTimer(time, false);
              } else {
                  startTimer(time, false);
              }
          }
  
          private void prepareView() {
  
          }
  
          private void setOnListener() {
  
              btnLunchOUT.setOnClickListener(new OnClickListener() {
                  @Override
                  public void onClick(View arg0) {
  
                      startTimer(time, true);
                      Handler handler = new Handler();
                      handler.postDelayed(new Runnable() {
                          public void run() {
                              btnLunchIN.setEnabled(true);
                          }
                      }, 2000);
  
  
  
  }
              });
  </code></pre>
  
  <p><strong>Here m storing the value of stopped time in shared preferences</strong></p>
  
  <pre><code>  btnLunchIN.setOnClickListener(new OnClickListener() {
  
                  @Override
                  public void onClick(View v) {
  
                      countdownTask.cancel();
                      btnLunchIN.setEnabled(false);
  
                      Handler handler = new Handler();
                      handler.postDelayed(new Runnable() {
                          public void run() {
                              btnLunchOUT.setEnabled(true);
                          }
                      }, 3000);
                      btnReset.setEnabled(true);
  
                      Toast.makeText(getApplicationContext(), "Timer Stopped",
                              Toast.LENGTH_SHORT).show();
                      flag = false;
  
                      pref = getPreferences(MODE_PRIVATE);**
                      SharedPreferences.Editor edit = pref.edit();
                      edit.putInt("key", time);
                      edit.commit();
  
                  }
              });
  </code></pre>
  
  <p><strong>** Getting Value of starting time from Shared Prefrences**</strong></p>
  
  <pre><code>            btnReset.setOnClickListener(new OnClickListener() {
  
                  @Override
                  public void onClick(View v) {
  
                      if (!flag) {
  
                          // countdownTask.cancel();
                          btnReset.setEnabled(false);
                          btnLunchOUT.setEnabled(true);
                          btnLunchIN.setEnabled(false);
                          flag = false;
                          time = 300;
                          number.setText("00:00:00");// Reset Timing Format
                          pref = getPreferences(MODE_PRIVATE);
                          SharedPreferences.Editor edit = pref.edit();
                          edit.putInt("key", 300);
                          edit.commit();
                      }
                  }
              });
          }
  
  
  
          @Override
          public void onResume() { ## OnResume method where i think its problem but i m unable to trace it
  
              System.out.println("In Resume");
              super.onResume();
              pref = getPreferences(MODE_PRIVATE);
              time = pref.getInt("key", time);
              number.setText("" + time);
  
              s = (time) % 60;
              m = ((time) / 60) % 60;
              h = 00;
              number.setText("0" + h + ":" + m + ":" + s);
  
  
  
              startTimer(time, false);
          }
  
  
      ## **onPause method** ##
          @Override
          public void onPause() { 
  
              System.out.println("Timer Pause:");
              pref = getPreferences(MODE_PRIVATE);
              SharedPreferences.Editor edit = pref.edit(); // Saving the value of timer in shared preferences
              edit.putInt("key", time);
              edit.commit();
  
              super.onPause();
  
          }
      ## Here my timer starts on start button click
          public void startTimer(int value, boolean isStartButton) {
  
              time = value;
              Log.e("ONMAIN:: ", "" + time);
              if (isStartButton) {
                  countdownTask = new TimerTask() {
                      @Override
                      public void run() {
                          runOnUiThread(new Runnable() {
                              @Override
                              public void run() {
  
                          time--; // ***Here i split 300 into HH:MM:SS format***
                              s = (time) % 60;
                                  m = ((time) / 60) % 60;
                                  h = 00;
  
                                  System.out.println("In Run" + time);
  </code></pre>
  
  <p><strong>> Enabled sound and vibrator on remianing 60 seconds</strong></p>
  
  <pre><code>                if (s &lt; 10) {
                                      if (m &lt; 10) {
                                          number.setText("0" + h + ":" + 0 + m + ":"
                                                  + 0 + s);
                                      }
                                  } else if (s &lt; 10) {
                                      number.setText("0" + h + ":" + m + ":" + 0 + s);
                                  } else if (m &lt; 10) {
                                      number.setText("0" + h + ":" + 0 + m + ":" + s);
                                  }
  
                                  else {
                                      number.setText("0" + h + ":" + m + ":" + s);
  
                                  }
                                  btnLunchOUT.setEnabled(false);
  
                                  btnReset.setEnabled(false);
  
                                  if (time == 60) {
                                      vibrate = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
  
                                      vibrate.vibrate(pattern, -1);
  
                                      soundalert.start();
                                      System.out.println("1 Minute Remianing ");
                                  }
  
                                  if (time == 1) {
  
                                      countdownTask.cancel();
  
  
                                  }
                              }
                          });
                      }
                  };
                  Timer countdown = new Timer(); //This is timer 
                  countdown.scheduleAtFixedRate(countdownTask, 0, 1000);
                  flag = false;
              }
          }
  
      }
  </code></pre>