我正在创建一个应用程序,它有一个控制countDownTimer的按钮,无论应用程序是否打开,它都会运行。在计时器结束时,状态栏中会弹出一条通知,提示用户启动另一项活动。该计时器以增加的间隔重复x次。另外,为了防止用户多次意外启动计时器,我在第一次按下后禁用该按钮。在倒计时重复循环结束时,按钮重新启用。
当我保持活动开放时,所有这一切都正常......
我的问题是当我按BACK时。如果我重新打开活动,则在倒计时周期结束后按钮无法重新启用。我相信当活动被破坏时,变量“count”的状态就会丢失。
我对编程很陌生,并会感谢任何帮助。我为这段代码的混乱感到抱歉!它仍然是一项正在进行中的工作。提前致谢!
@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.makeText(getApplicationContext(),“Times Up”+ String.valueOf(times [count]),Toast.LENGTH_SHORT); // t.show(); 计数++;
if (count < 3)
{
countDownTimer = new MyCountDownTimer(times[count], interval);
countDownTimer.start();
}
else
{
count=0;
startB.setEnabled(true);
timerHasStarted=false;
}
}
答案 0 :(得分:0)
我不知道问题是否仍然相关。
您应该检查变量mCount出现的位置。当您使用LoadPreferences时,您会读到&#34; mCount&#34;进入mCount,使用其未定义的值作为默认值。然后通过执行mCount = count来删除读取值。你的意思是count = mCount吗?
在onCreate中的LoadPreferences之后,只根据savedInstanceState设置count。因此无论LoadPreferences做什么计数都会被忽略。按BACK后杀死它后启动活动时,savedInstanceState将为null,但如果首选项另有说明,则您不希望count为0。
答案 1 :(得分:0)
而不是使用取消方法倒计时器并保存剩余时间然后在恢复活动方法中启动一个新的倒计时器