0

i want that my countdown continue when i completely close the app but i dont know how to do it. Someone can explain it to me or pass me some link where explain it? Thank

The code for the countdown

public class MainActivity extends Activity {
Button b1;
TextView tv1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tv1 = (TextView) findViewById(R.id.tv1);
    b1 = (Button) findViewById(R.id.b1);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu
    // this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

public void a(View view){
    new CountDownTimer(10000, 1000) {
        public void onTick(long millisUntilFinished) {
            tv1.setText("La cuenta llega a 0 en: " + millisUntilFinished / 1000);
        }
        public void onFinish() {
            tv1.setText("Listo!");
        }
    }.start();
}

}

Agustin Val
  • 187
  • 7

1 Answers1

0

You need to do following steps : At the exit point of your app(either it is onBackPress of Home Activity or Ok click of the dialog that ask for exit) find the value of System.currentTimeMillis() and save it to SharedPrefences.

Now on second launch(in onCreate()) just find again value of System.currentTimeMillis(), then get the data saved in SharedPrefences and get the diff. of both values.

Now the diff is the time period that your app was shut down, hope you are looking for the same data.

Neo
  • 3,546
  • 1
  • 24
  • 31