继续倒计时

时间:2016-06-14 23:00:19

标签: android 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();
}

}

1 个答案:

答案 0 :(得分:0)

您需要执行以下步骤:  在您的应用程序的退出点(要么是onBackPress of Home Activity或Ok点击要求退出的对话框),找到System.currentTimeMillis()的值并将其保存到SharedPrefences。

现在第二次启动(在onCreate()中)再次找到System.currentTimeMillis()的值,然后获取保存在SharedPrefences中的数据并获取差异。两个价值观。

现在差异是应用关闭的时间段,希望您正在寻找相同的数据。