Timer Android在活动销毁后重新创建时保存变量
我正在创建一个应用程序,它有一个控制倒计时的按钮,无论应用程序是否打开,倒计时都会运行。在计时器结束时,状态栏中会弹出一个通知,提示用户启动另一个活动。此计时器以递增的间隔重复x次。另外,为了防止用户意外启动计时器多次,我在第一次按下后禁用了按钮。在倒计时重复周期结束时,按钮重新启用 当我保持活动开放时,所有这些都很好 我的问题是当我按下按钮时。如果我重新打开活动,则在倒计时周期结束后,按钮无法重新启用。我认为,当活动被破坏时,变量计数的状态将丢失 我对编程很陌生,希望能得到任何帮助。很抱歉,这段代码太乱了!这在很大程度上仍然是一项正在进行的工作。提前谢谢Timer Android在活动销毁后重新创建时保存变量,timer,savestate,Timer,Savestate,我正在创建一个应用程序,它有一个控制倒计时的按钮,无论应用程序是否打开,倒计时都会运行。在计时器结束时,状态栏中会弹出一个通知,提示用户启动另一个活动。此计时器以递增的间隔重复x次。另外,为了防止用户意外启动计时器多次,我在第一次按下后禁用了按钮。在倒计时重复周期结束时,按钮重新启用 当我保持活动开放时,所有这些都很好 我的问题是当我按下按钮时。如果我重新打开活动,则在倒计时周期结束后,按钮无法重新启用。我认为,当活动被破坏时,变量计数的状态将丢失 我对编程很陌生,希望能得到任何帮助。很抱歉,这
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startB = (Button) this.findViewById(R.id.button);
startB.setOnClickListener(this);
countDownTimer = new MyCountDownTimer(times[count], interval);
LoadPreferences();
if (savedInstanceState !=null)
{
count = savedInstanceState.getInt("mCount");
}
else
{
count=0;
}
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState)
{
savedInstanceState.putInt("mCount", count);
super.onSaveInstanceState(savedInstanceState);
}
@Override
public void onBackPressed(){
SavePreferences();
super.onBackPressed();
}
private void SavePreferences(){
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("mCount", mCount);
editor.putBoolean("state", startB.isEnabled());
editor.commit();
}
private void LoadPreferences(){
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
int mCount = sharedPreferences.getInt("count", mCount);
Boolean state = sharedPreferences.getBoolean("state",true);
startB.setEnabled(state);
mCount = count
}
public void onClick(View v)
{
if (!timerHasStarted)
{
countDownTimer.start();
timerHasStarted = true;
Toast t= Toast.makeText(this,"Timer Started", Toast.LENGTH_SHORT);
t.show();
}
else
{
startB.setEnabled(false);
}
}
// CountDownTimer class
public class MyCountDownTimer extends CountDownTimer
{
public MyCountDownTimer(long startTime, long interval)
{
super(startTime, interval);
}
@Override
public void onFinish()
{
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getBaseContext())
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("ASL Teacher")
.setContentText("Play Lesson One");
//Intent resultIntent= new Intent();
//TaskStackBuilder stackBuilder = TaskStackBuilder.create(MainActivity.this);
//stackBuilder.addParentStack(NextActivity.class);
//stackBuilder.addNextIntent(resultIntent);
//PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
//mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(0, mBuilder.build());
Vibrator v= (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(pattern,-1);
//Toast t=Toast.makeTextgetApplicationContext,Times Up+String.valueOftimes[count],Toast.LENGTH\u SHORT;
//t.show;
计数++
if (count < 3)
{
countDownTimer = new MyCountDownTimer(times[count], interval);
countDownTimer.start();
}
else
{
count=0;
startB.setEnabled(true);
timerHasStarted=false;
}
}
我不知道这个问题是否仍然相关 您应该检查变量mCount出现的位置。加载Preferences时,使用其未定义的值作为默认值,将mCount读入mCount。然后通过执行mCount=count删除读取值。你是说count=mCount吗
在onCreate中加载Preferences后,仅根据savedInstanceState设置计数。因此,LoadPreferences对计数所做的任何操作都将被忽略。当您通过按“上一步”终止活动后启动该活动时,savedInstanceState将为空,但如果首选项另有规定,则您不希望计数为0。使用倒计时的“取消”方法并保存剩余时间,然后在“活动”的“恢复”方法中启动新的倒计时