如何在用户关闭屏幕时暂停游戏

时间:2015-04-17 21:07:27

标签: java android eclipse

我的某个游戏有问题。这是基于时间的益智游戏,我有它的问题。当用户按下Android设备上的开/关按钮时,游戏不会停止,但计时器会一直持续到游戏结束。当用户再次打开屏幕时,他可以在屏幕上看到游戏。但是我希望在用户按下开/关按钮游戏时暂停。

有什么建议吗?我在编程方面很新,所以请解释一下这个非常基本的方法。

谢谢大家!

编辑。定时器代码

private void initializeProgressBar() {
    //initialize progressbar
    progress = ApplicationConstants.GAME_TIME;

    mProgress = (ProgressBarDetermininate) findViewById(R.id.progressDeterminate);
    mProgress.setMax(progress);
    mProgress.setProgress(progress );
    timer = new Timer();
    progressBarUpdateTask = new ProgressBarUpdateTask();
    timer.schedule(progressBarUpdateTask, 20, 20);
}

class ProgressBarUpdateTask extends TimerTask {
      @Override
      public void run() {
       runOnUiThread(new Runnable(){
        @Override
        public void run() {
            progress-=1;
            if(progress==0)
            {
                TimeOver();
            }
            mProgress.setProgress(progress);
        }

        });
      }

    @Override
protected void onResume() {
    super.onResume();


}

@Override
protected void onPause() {
    super.onPause();
    this.timer.cancel();

} 

4 个答案:

答案 0 :(得分:4)

在游戏运行的“活动”上下文中,以onStop()onPause()(根据您的需要)暂停游戏。

答案 1 :(得分:1)

我假设你正在使用android的活动......

@Override
protected void onResume() {
    super.onResume();
    // Resume the timer or show a button for the user to press when ready
    // !!! Also check if timer exits because onResume is called before onCreate!!!
}

@Override
protected void onPause() {
    super.onPause();
    // Pause the timer
}

答案 2 :(得分:1)

执行此操作的一种方法是检测用户状态,这是一个示例

在游戏开始时LockService

startService(new Intent(getApplicationContext(), LockService.class));

<强> LockService.java

public class LockService extends Service {
    @Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {
super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_USER_PRESENT);
final BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
return super.onStartCommand(intent, flags, startId);
}
public class LocalBinder extends Binder {
LockService getService() {
return LockService.this;
}
}
}

然后最后是你可以停止游戏的BroadcastReceiver

<强> ScreenReceiver.java

public class ScreenReceiver extends BroadcastReceiver {

    public static boolean wasScreenOn = true;

    @Override
    public void onReceive(final Context context, final Intent intent) {
    Log.e("LOB","onReceive");
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            // do whatever you need to do here
            wasScreenOn = false;

           /* PAUSE THE GAME HERE*/

            Log.e("LOB","wasScreenOn"+wasScreenOn);
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            // and do whatever you need to do here
            wasScreenOn = true;
        }else if(intent.getAction().equals(Intent.ACTION_USER_PRESENT)){

        }
    }
}

正如其他答案中正确提到的,您也可以使用onPause()方法

@Override
protected void onPause() {
    super.onPause();
    // stop the game
}

答案 3 :(得分:1)

当您按下屏幕或电源按钮时,将调用应用程序的暂停方法,当您再次按电源按钮时,将调用应用程序onResume方法,您应该暂停onPause中的计时器并在onResume中恢复它。