我正在创建一个应用程序,该应用程序有一个控制 countDownTimer 的按钮,无论应用程序是否打开,它都会运行。在计时器结束时,状态栏中会弹出一条通知,提示用户开始另一个活动。该计时器以递增的间隔重复 x 次。此外,为了防止用户多次意外启动计时器,我在第一次按下后禁用了该按钮。在倒计时重复循环结束时,按钮重新启用。
当我保持活动打开时,所有这些都很好......
我的问题是当我按 BACK 时。如果我重新打开活动,则在倒计时周期结束后按钮无法重新启用。我相信当活动被破坏时,变量“计数”的状态就会丢失。
我对编程很陌生,希望能提供任何帮助。对于这段代码的混乱程度,我深表歉意!这仍然是一项正在进行中的工作。提前致谢!
@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;
}
}