Java 按下home(主页)按钮时如何防止onPause()?
我正在开发一个包含计时器的libGDX应用程序。我需要定时器运行,即使按下home按钮,即使按下onoff按钮关闭屏幕 为此,我考虑阻止应用程序进入暂停状态。 在AndroidLauncher.java中,我添加了以下代码行:Java 按下home(主页)按钮时如何防止onPause()?,java,android,libgdx,onpause,Java,Android,Libgdx,Onpause,我正在开发一个包含计时器的libGDX应用程序。我需要定时器运行,即使按下home按钮,即使按下onoff按钮关闭屏幕 为此,我考虑阻止应用程序进入暂停状态。 在AndroidLauncher.java中,我添加了以下代码行: @Override public void onPause() { onResume(); } 这个想法是在应用程序进入暂停状态后立即恢复 以下是按下home(主页)按钮并返回应用程序后的应用程序日志: 01-26 18:17:25.125:I/
@Override
public void onPause() {
onResume();
}
这个想法是在应用程序进入暂停状态后立即恢复
以下是按下home(主页)按钮并返回应用程序后的应用程序日志:
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/IIInputConnectionWrapper(2753):在非活动输入连接上显示状态图标
01-26 18:17:28.899:I/AndroidInput(2753):传感器
01-26 18:17:28.919:I/AndroidGraphics(2753):继续
如您所见,当应用程序运行时,如果我按下home(主页)按钮,应用程序将恢复,而不会进入暂停状态,当我返回应用程序时,它将再次恢复。但是,计时器停止了
为什么当应用程序从未处于暂停状态时计时器停止?
最后,按下home(主页)按钮后,如何保持计时器运行
谢谢
编辑: 所以我开始使用这些服务,但在后台运行计时器时仍然会遇到问题 这是开始日期:
timer = new Timer();
timer.scheduleTask(new Task(){
public void run() {
timerSeconds--;
}
}, 1,1);
public interface ActionResolver {
public void backgroundTimer(int a);
}
@Override
public void pause() {
super.pause();
actionResolver.backgroundTimer(GameScreen.timerSeconds);
}
@Override
public void backgroundTimer(int a) {
aa = a;
Intent intentTimer = new Intent(AndroidLauncher.this, IntentServiceTimer.class);
intentTimer.putExtra(TIMER, aa);
startService(intentTimer);
}
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);
}
}
<service android:name="IntentServiceTimer"/>
- 在我的游戏屏幕上,我有一个计时器,在那里我可以输入一些数字 秒
- 然后我使用任务倒计时到0
- 当我在一个libGDX项目上工作时,我使用一个接口来 将当前秒数传送给AndroidLauncher, 当游戏进入暂停状态时
- 该接口还触发一个保持倒计时的服务 使用相同的计时器和任务运行,并每秒在控制台中打印当前秒数
timer = new Timer();
timer.scheduleTask(new Task(){
public void run() {
timerSeconds--;
}
}, 1,1);
public interface ActionResolver {
public void backgroundTimer(int a);
}
@Override
public void pause() {
super.pause();
actionResolver.backgroundTimer(GameScreen.timerSeconds);
}
@Override
public void backgroundTimer(int a) {
aa = a;
Intent intentTimer = new Intent(AndroidLauncher.this, IntentServiceTimer.class);
intentTimer.putExtra(TIMER, aa);
startService(intentTimer);
}
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);
}
}
<service android:name="IntentServiceTimer"/>
GameScreen.java中的计时器:
timer = new Timer();
timer.scheduleTask(new Task(){
public void run() {
timerSeconds--;
}
}, 1,1);
public interface ActionResolver {
public void backgroundTimer(int a);
}
@Override
public void pause() {
super.pause();
actionResolver.backgroundTimer(GameScreen.timerSeconds);
}
@Override
public void backgroundTimer(int a) {
aa = a;
Intent intentTimer = new Intent(AndroidLauncher.this, IntentServiceTimer.class);
intentTimer.putExtra(TIMER, aa);
startService(intentTimer);
}
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);
}
}
<service android:name="IntentServiceTimer"/>
接口ActionResolver.java:
timer = new Timer();
timer.scheduleTask(new Task(){
public void run() {
timerSeconds--;
}
}, 1,1);
public interface ActionResolver {
public void backgroundTimer(int a);
}
@Override
public void pause() {
super.pause();
actionResolver.backgroundTimer(GameScreen.timerSeconds);
}
@Override
public void backgroundTimer(int a) {
aa = a;
Intent intentTimer = new Intent(AndroidLauncher.this, IntentServiceTimer.class);
intentTimer.putExtra(TIMER, aa);
startService(intentTimer);
}
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);
}
}
<service android:name="IntentServiceTimer"/>
在主活动MyGdxGame.java中调用ActionResolver:
timer = new Timer();
timer.scheduleTask(new Task(){
public void run() {
timerSeconds--;
}
}, 1,1);
public interface ActionResolver {
public void backgroundTimer(int a);
}
@Override
public void pause() {
super.pause();
actionResolver.backgroundTimer(GameScreen.timerSeconds);
}
@Override
public void backgroundTimer(int a) {
aa = a;
Intent intentTimer = new Intent(AndroidLauncher.this, IntentServiceTimer.class);
intentTimer.putExtra(TIMER, aa);
startService(intentTimer);
}
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);
}
}
<service android:name="IntentServiceTimer"/>
后台计时器方法在AndroidLauncher中定义:
timer = new Timer();
timer.scheduleTask(new Task(){
public void run() {
timerSeconds--;
}
}, 1,1);
public interface ActionResolver {
public void backgroundTimer(int a);
}
@Override
public void pause() {
super.pause();
actionResolver.backgroundTimer(GameScreen.timerSeconds);
}
@Override
public void backgroundTimer(int a) {
aa = a;
Intent intentTimer = new Intent(AndroidLauncher.this, IntentServiceTimer.class);
intentTimer.putExtra(TIMER, aa);
startService(intentTimer);
}
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);
}
}
<service android:name="IntentServiceTimer"/>
然后我创建了intentServiceTimer类:
timer = new Timer();
timer.scheduleTask(new Task(){
public void run() {
timerSeconds--;
}
}, 1,1);
public interface ActionResolver {
public void backgroundTimer(int a);
}
@Override
public void pause() {
super.pause();
actionResolver.backgroundTimer(GameScreen.timerSeconds);
}
@Override
public void backgroundTimer(int a) {
aa = a;
Intent intentTimer = new Intent(AndroidLauncher.this, IntentServiceTimer.class);
intentTimer.putExtra(TIMER, aa);
startService(intentTimer);
}
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);
}
}
<service android:name="IntentServiceTimer"/>
在AndroidManifest.xml中,我声明了服务:
timer = new Timer();
timer.scheduleTask(new Task(){
public void run() {
timerSeconds--;
}
}, 1,1);
public interface ActionResolver {
public void backgroundTimer(int a);
}
@Override
public void pause() {
super.pause();
actionResolver.backgroundTimer(GameScreen.timerSeconds);
}
@Override
public void backgroundTimer(int a) {
aa = a;
Intent intentTimer = new Intent(AndroidLauncher.this, IntentServiceTimer.class);
intentTimer.putExtra(TIMER, aa);
startService(intentTimer);
}
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);
}
}
<service android:name="IntentServiceTimer"/>
但是,计时器循环(包括倒计时和每秒钟打印一次倒计时)似乎不起作用,因为控制台中没有打印任何内容。但是,如果我回到应用程序,countown每秒钟打印一次游戏处于暂停状态。例如,如果我将游戏置于暂停状态5秒钟,只要我返回应用程序,它就会打印:
剩余秒数:27
剩余秒数:26
剩余秒数:25
剩余秒数:24
剩余秒数:23
所以我有一种奇怪的服务行为:
- 按预期打印第一条消息(当游戏处于暂停状态时)
- 倒计时似乎是在后台进行的
- 只有在恢复时才会打印倒计时结果
我仍然没有检查alarmmanager,因为我需要在后台运行更详细的流程,将来,这些服务似乎就是我所需要的。您应该使用服务来解决此问题: 当您按下三个软按钮中的一个时调用的另一个方法是onUserLeaveHint,但它不能解决您的问题,您可以在将来使用它。当然:
protected void onUserLeaveHint() {
super.onUserLeaveHint();
}
您应该使用服务来解决此问题: 当您按下三个软按钮中的一个时调用的另一个方法是onUserLeaveHint,但它不能解决您的问题,您可以在将来使用它。当然:
protected void onUserLeaveHint() {
super.onUserLeaveHint();
}
好的!因此,在为这个问题疯狂了一整天之后,我终于找到了(非常简单的)解决方案: 在编辑我最初的问题时,我开始使用服务,但我有一个非常奇怪的服务行为。我的问题来自这样一个事实:在我的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);
}
}
好的!因此,在为这个问题疯狂了一整天之后,我终于找到了(非常简单的)解决方案: 在编辑我最初的问题时,我开始使用服务,但我有一个非常奇怪的服务行为。我的问题来自这样一个事实:在我的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);
}
}
因为在Resume上调用
不会强制你的应用程序保持运行。如果您的活动在后台,则会暂停。您需要做的是将计时器的状态与时间一起保存在onPause
中,以便在用户返回应用程序时正确恢复。如果您需要在活动处于非活动状态时触发计时器,则需要alarmmanager或服务。对于任何与时间相关的任务,您应该使用AlarmManager
谢谢您的评论。我检查了似乎是解决方案的服务,但我仍然需要一些帮助来帮助服务正常运行。我用新代码更新了我的问题。有什么想法吗?谢谢因为在Resume上调用不会强制你的应用程序保持运行。如果您的活动在后台,则会暂停。您需要做的是将计时器的状态与时间一起保存在onPause
中,以便在用户返回应用程序时可以正确恢复计时器。如果您需要计时器