Java 当用户关闭屏幕时如何暂停游戏
我的一个游戏有问题。这是一款基于时间的益智游戏,我有问题。当用户按下安卓设备上的开/关按钮时,游戏不会停止,但计时器会一直持续到游戏结束。当用户再次打开屏幕时,他可以在屏幕上看到游戏。但我想做一个场景,当用户按下开/关按钮时,游戏将暂停 有什么建议吗?我是编程新手,所以请给我解释一下最基本的编程方法 谢谢大家 编辑。计时器代码Java 当用户关闭屏幕时如何暂停游戏,java,android,eclipse,Java,Android,Eclipse,我的一个游戏有问题。这是一款基于时间的益智游戏,我有问题。当用户按下安卓设备上的开/关按钮时,游戏不会停止,但计时器会一直持续到游戏结束。当用户再次打开屏幕时,他可以在屏幕上看到游戏。但我想做一个场景,当用户按下开/关按钮时,游戏将暂停 有什么建议吗?我是编程新手,所以请给我解释一下最基本的编程方法 谢谢大家 编辑。计时器代码 private void initializeProgressBar() { //initialize progressbar progress = Ap
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();
}
根据您在游戏运行的活动环境中的需要,将游戏暂停在桌面上或暂停上。我假设您正在使用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
}
一种方法是检测用户的存在,下面是一个示例 在游戏开始时,启动锁定服务 LockService.java 最后是广播接收器,您可以在其中停止游戏 ScreenReceiver.java 正如其他答案中正确提到的,您也可以使用该方法
当您按下屏幕关闭或电源按钮打开时,将调用应用程序的pause方法,当您再次按下电源按钮时,将调用应用程序的onResume方法,您应该在onPause中暂停计时器,并在onResume中恢复它。我添加了这个。timer.cancel;在暂停中。。。但是如何恢复计时器呢?根据文档:当计时器不再需要时,用户应该调用cancel,这将释放计时器的线程和其他资源。未显式取消的计时器可能无限期地占用资源。因此,在调用cancel后,您无法恢复java.util.Time。只需在OnResume中创建计时器,谢谢你,alenz316。现在我在简历中加入了计时器代码。但当我关闭屏幕并再次打开时,计时器会从头开始重新启动。这将是很好的作弊游戏,如果用户会发现:任何建议?只要节省时间,当你取消计时器,然后使用它当你创建一个新的;
startService(new Intent(getApplicationContext(), LockService.class));
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;
}
}
}
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)){
}
}
}
@Override
protected void onPause() {
super.onPause();
// stop the game
}