在暂停/简历已经结束后,请勿重置计时器(难以解释)
我是java android的新手。我对 SO 做了很多研究,但找不到与我匹配的案例。
我用不同的计时器制作了一个 Android 应用程序游戏。我实现了暂停/恢复方法,它工作正常,但第二个计时器有一个大问题:我希望这个计时器在活动周期已经完成时不要从暂停中恢复。该计时器对应于游戏中角色的动作。我希望我能很好地解释自己(抱歉我的英语,我是法国人)。非常感谢您的帮助...
我的暂停:
protected void onPause() {
super.onPause();
MyMediaPlayer.getMediaPlayerInstance().stopAudioFile();
SharedPreferences prefs = getSharedPreferences("X", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("lastActivity", getClass().getName());
editor.commit();
mCountDownTimer.cancel();
mCountDownTimer = null;
Timer2.cancel();
Timer2 = null;
}
我的恢复:
protected void onResume() {
super.onResume();
MyMediaPlayer.getMediaPlayerInstance().resumeAudio();
if (mCountDownTimer == null) {
mCountDownTimer = new CountDownTimer(mTimeRemaining, TIMER_INTERVAL) {
@Override
public void onTick(long millisUntilFinished) {
textView.setText(String.format(Locale.getDefault(), "%d sec.", millisUntilFinished / 1000L));
mTimeRemaining = millisUntilFinished;
}
@Override
public void onFinish() {
Intent intent = new Intent(P39x.this, Lose15b.class);
Toast toast = Toast.makeText(P39x.this, "time has passed...\n" +
"\n" +
"YOU HAVE LOST !", Toast.LENGTH_LONG);
View toastView = toast.getView();
TextView toastMessage = (TextView) toastView.findViewById(android.R.id.message);
Typeface typeface = Typeface.createFromAsset(getAssets(), "SanvitoProLight.ttf");
toastMessage.setTypeface(typeface);
toastMessage.setTextSize(50);
toastMessage.setBackgroundResource(R.drawable.quiet);
toastMessage.setTextColor(Color.RED);
toastMessage.setGravity(Gravity.CENTER_VERTICAL);
toast.show();
getMediaPlayerInstance().stopAudioFile();
startActivity(intent);
finish();
MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.collapse);
mediaPlayer.start();
}
}.start();
if (Timer2 == null) {
Timer2 = new CountDownTimer(TIMERDURATION, TIMERINTERVAL) {
@Override
public void onTick(long millisUntilFinished) {
mTimeRemainin = millisUntilFinished;
}
@Override
public void onFinish() {
toast = Toast.makeText(P39x.this, "Find your way now", Toast.LENGTH_LONG);
View toastView = toast.getView();
TextView toastMessage = (TextView) toastView.findViewById(android.R.id.message);
Typeface typeface = Typeface.createFromAsset(getAssets(), "SanvitoProLight.ttf");
toastMessage.setTypeface(typeface);
toastMessage.setTextSize(40);
toastMessage.setBackgroundColor(Color.BLACK);
toastMessage.setTextColor(Color.MAGENTA);
toastMessage.setGravity(Gravity.CENTER_VERTICAL);
toast.show();
}
}.start();
我希望 Timer2 在活动周期已完成时不要从暂停中恢复。
I'm a newbie in java android. I've done a lot of research on SO but couldn't find a case that matches mine.
I made an android app game with different timers. I implemented the pause/resume method, it works fine but big problem with the second timer: i would like this timer not to resume in return from a pause when it is already finished in the cycle of the activity. This timer corresponds to the action of a character in the game. I hope I explained myself well (sorry for my English, I'm French). thanks a lot for your help...
My ON PAUSE:
protected void onPause() {
super.onPause();
MyMediaPlayer.getMediaPlayerInstance().stopAudioFile();
SharedPreferences prefs = getSharedPreferences("X", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("lastActivity", getClass().getName());
editor.commit();
mCountDownTimer.cancel();
mCountDownTimer = null;
Timer2.cancel();
Timer2 = null;
}
My ON RESUME:
protected void onResume() {
super.onResume();
MyMediaPlayer.getMediaPlayerInstance().resumeAudio();
if (mCountDownTimer == null) {
mCountDownTimer = new CountDownTimer(mTimeRemaining, TIMER_INTERVAL) {
@Override
public void onTick(long millisUntilFinished) {
textView.setText(String.format(Locale.getDefault(), "%d sec.", millisUntilFinished / 1000L));
mTimeRemaining = millisUntilFinished;
}
@Override
public void onFinish() {
Intent intent = new Intent(P39x.this, Lose15b.class);
Toast toast = Toast.makeText(P39x.this, "time has passed...\n" +
"\n" +
"YOU HAVE LOST !", Toast.LENGTH_LONG);
View toastView = toast.getView();
TextView toastMessage = (TextView) toastView.findViewById(android.R.id.message);
Typeface typeface = Typeface.createFromAsset(getAssets(), "SanvitoProLight.ttf");
toastMessage.setTypeface(typeface);
toastMessage.setTextSize(50);
toastMessage.setBackgroundResource(R.drawable.quiet);
toastMessage.setTextColor(Color.RED);
toastMessage.setGravity(Gravity.CENTER_VERTICAL);
toast.show();
getMediaPlayerInstance().stopAudioFile();
startActivity(intent);
finish();
MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.collapse);
mediaPlayer.start();
}
}.start();
if (Timer2 == null) {
Timer2 = new CountDownTimer(TIMERDURATION, TIMERINTERVAL) {
@Override
public void onTick(long millisUntilFinished) {
mTimeRemainin = millisUntilFinished;
}
@Override
public void onFinish() {
toast = Toast.makeText(P39x.this, "Find your way now", Toast.LENGTH_LONG);
View toastView = toast.getView();
TextView toastMessage = (TextView) toastView.findViewById(android.R.id.message);
Typeface typeface = Typeface.createFromAsset(getAssets(), "SanvitoProLight.ttf");
toastMessage.setTypeface(typeface);
toastMessage.setTextSize(40);
toastMessage.setBackgroundColor(Color.BLACK);
toastMessage.setTextColor(Color.MAGENTA);
toastMessage.setGravity(Gravity.CENTER_VERTICAL);
toast.show();
}
}.start();
I would like Timer2 not to resume in return from a pause when it is already finished in the cycle of the activity.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我正确理解您的问题,您希望第二个计时器如果已经完成该活动,则不重新启动。我认为有不同的方法可以解决这个问题。解决方案可能是实施一个布尔变量,该变量是否应持续到计时器是否继续进行信息。
在timer2 onfinish()函数中,您将此var设置为false
,在onResume()函数中,您可以使用它:
If I understand your problem correctly, you want that the second timer not to restart if it has already finished at the activity. I think there are different ways to solve this problem. A solution could be to implement a boolean variable that holds the information whether the timer should continue or not.
In the Timer2 onFinish() function you set this var to false
and in the onResume() function you use it:
这是一个生命周期问题,将timer2移至onCreate()方法而不是onresume,因为on resume在Onpause()处于活动状态后被调用。
This is a lifecycle issue, move timer2 to onCreate() method instead of onResume because onResume get called after onPause() is active.