【问题标题】:How to prevent onPause() when pressing home button?按下主页按钮时如何防止onPause()?
【发布时间】:2015-01-26 23:24:54
【问题描述】:

我正在开发一个包含计时器的 libGDX 应用程序。即使按下主页按钮,即使按下开关按钮关闭屏幕,我也需要计时器运行。

为此,我考虑过阻止应用进入暂停状态。 在 AndroidLauncher.java 我添加这行代码:

@Override
  public void onPause() {
        onResume();
  }

这个想法是在应用程序进入暂停状态后立即恢复它。

这是按home键返回应用后应用的logcat:

01-26 18:17:25.125:I/AndroidInput(2753):传感器侦听器设置

01-26 18:17:25.126:I/AndroidGraphics(2753):恢复

01-26 18:17:25.191:W/IInputConnectionWrapper(2753):非活动 InputConnection 上的 showStatusIcon

01-26 18:17:28.899:I/AndroidInput(2753):传感器

01-26 18:17:28.919:I/AndroidGraphics(2753):恢复

如您所见,当应用程序运行时,如果我按下主页按钮,应用程序会恢复,不会进入暂停状态,当我返回应用程序时,它会再次恢复。但是,计时器停止了......

为什么应用程序从来没有处于暂停状态时计时器停止了? 最后,如何在按下主页按钮后保持计时器运行?

谢谢


编辑: 所以我开始使用这些服务,但我仍然遇到让计时器在后台运行的问题。

这里是攻略:

  • 在我的 GameScreen 中,我有一个计时器,我可以在其中输入多个 秒,
  • 然后我使用任务倒计时到 0。
  • 当我在做一个 libGDX 项目时,我使用一个接口来 将当前秒数传达给 AndroidLauncher, 当游戏进入暂停状态时。
  • 该界面还触发了一个保持倒计时的服务 使用相同的计时器和任务运行,它每秒钟在控制台中打印当前的秒数。

代码:

GameScreen.java 中的计时器:

timer = new Timer();
timer.scheduleTask(new Task(){
        public void run() {
            timerSeconds--;
        }
    }, 1,1);

接口 ActionResolver.java :

public interface ActionResolver {
    public void backgroundTimer(int a);
}

ActionResolver 在主Activity MyGdxGame.java 中被调用:

@Override
public void pause() {
    super.pause();
    actionResolver.backgroundTimer(GameScreen.timerSeconds);
}

backgroundTimer 方法在 AndroidLauncher 中定义:

@Override
public void backgroundTimer(int a) {
    aa = a;
    
    Intent intentTimer = new Intent(AndroidLauncher.this, IntentServiceTimer.class);
    intentTimer.putExtra(TIMER, aa);
    startService(intentTimer);
}

然后我创建了 intentServiceTimer 类:

public class IntentServiceTimer extends IntentService{
private final static String TAG = "IntentServiceExample";
int a;

public IntentServiceTimer(){
    super(TAG);
}

@Override
protected void onHandleIntent(final Intent intent) {
    Log.d(TAG, "When the game went on pause, the number of remaining seconds was : " + intent.getIntExtra(AndroidLauncher.TIMER, -1));
    
    a = intent.getIntExtra(AndroidLauncher.TIMER, -1);
    Timer timer = new Timer();
    timer.scheduleTask(new Task(){
        public void run() {
           a--;
           Log.d(TAG, "Seconds remaining : " + a);
        }
    }, 1,1);        
}
}

我在 AndroidManifest.xml 中声明了服务:

<service android:name="IntentServiceTimer"/>

结果:

当我午餐应用程序时,只要游戏进入暂停状态,服务的这行就会打印正确的消息,这很好:

Log.d(TAG, "When the game went on pause, the number of remaining seconds was : " + intent.getIntExtra(AndroidLauncher.TIMER, -1));

但是,包含倒计时和每秒钟打印一次倒计时的计时器循环似乎不起作用,因为控制台中没有打印任何内容。但是,如果我回到应用程序,countown 会在一段时间内打印游戏处于暂停状态的每一秒。例如,如果我将游戏置于暂停状态 5 秒,一旦我回到应用程序,它就会打印:

剩余秒数:27

剩余秒数:26

剩余秒数:25

剩余秒数:24

剩余秒数:23

所以我有这种奇怪的服务行为:

  • 按预期打印第一条消息(当游戏进入暂停状态时)
  • 倒计时似乎在后台运行
  • 倒计时结果仅在恢复时打印

您对这个问题有任何想法吗? 我仍然没有检查警报管理器,因为我需要在后台运行更精细的进程,将来,这些服务似乎是我所需要的。

【问题讨论】:

  • 因为调用onResume 不会强制您的应用保持正常运行。如果您的活动在后台,则会暂停。你要做的就是把你的定时器的状态和时间一起保存在onPause中,这样当用户回到应用程序时你可以正确地恢复它。
  • 如果您还需要在活动不活动时触发计时器,那么您需要警报管理器或服务。
  • 对于任何与时间相关的任务,您应该使用AlarmManager
  • 感谢您的 cmets。我检查了似乎是解决方案的服务,但我仍然需要一些帮助来帮助服务正常运行。我用新代码更新了我的问题。任何的想法 ?谢谢!

标签: java android libgdx onpause


【解决方案1】:

你应该使用服务来解决这个问题:Services

当你按下三个软按钮时调用的其他方法是onUserLeaveHint,但它不能解决你的问题,顺便说一句你将来可以使用它:

    protected void onUserLeaveHint() {
        super.onUserLeaveHint();
    }

【讨论】:

  • 感谢您的回答!这些服务似乎是我所需要的,但我的服务有一种奇怪的行为。我用新代码更新了我最初的问题。如果您知道我的代码中缺少的一些小东西可以正常运行,那就太好了!谢谢!
【解决方案2】:

好的!所以,在为这个问题生气了一整天之后,我终于找到了(非常简单的)解决方案:

在编辑我最初的问题时,我开始使用服务,但我对服务的行为非常奇怪。我的问题来自这样一个事实,即在我的 IntentService 类中,我使用了 libgdx 包中的 Timer() 和 Task() 方法。

我只是用 java.util 包中的等效 Timer() 和 TimerTask() 方法替换了这两个类,并且一切工作顺利!

所以现在,我的 IntentServiceTimer 看起来像这样:

import java.util.Timer;
import java.util.TimerTask;
import android.app.IntentService;
import android.content.Intent;
import android.util.Log;

public class IntentServiceTimer extends IntentService{
private final static String TAG = "IntentServiceExample";
int a;

public IntentServiceTimer(){
    super(TAG);
}

@Override
protected void onHandleIntent(final Intent intent) {
    Log.d(TAG, "When the game went on pause, the number of remaining seconds was : " + intent.getIntExtra(AndroidLauncher.TIMER, -1));

    a = intent.getIntExtra(AndroidLauncher.TIMER, -1);
    Timer timer = new Timer();
    timer.schedule(new TimerTask(){
        public void run() {
           a--;
           Log.d(TAG, "Seconds remaining : " + a);
        }
    }, 1,1);        
}
}

【讨论】:

    猜你喜欢
    • 2021-10-05
    • 2011-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多