0

앱이 완전히 닫히면 카운트 다운 타이머가 일시 중지되고 저장되는 카운트 다운을 만들고 싶습니다. 앱을 다시 열면 중단 된 부분부터 카운트 다운이 재개됩니다. 내 생각은 앱이 닫힐 때 "onStop"에 "millisUntilFinished"값을 저장하고 앱을 열 때 "onResume"에서 카운트 다운을 계속하는 것입니다. 문제는 내가 어떻게하는지, 누군가 도움이되는지를 모른다는 것입니까?앱 수명주기 동안 가치 유지 - 카운트 다운 타이머 일시 중지/재개

내 카운트 다운의 코드 :

public class MainActivity extends Activity { 
    Button b1; 

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

    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

[SharedPreferences] (https://developer.android.com/training/basics/data-storage/shared-preferences.html)를 사용하십시오. –

답변