I've noticed on several Android (KitKat) phones that the low battery popup does not call APPLICATION_SUSPEND or any events.
I'm guessing we'll need to hook into the battery information and pause if it detects a change to low battery. OR - maybe that was already coded and there is a bug?
Comments
http://docs.giderosmobile.com/reference/gideros/EventDispatcher/Event.MEMORY_WARNING
I never test this. I rarely use it, i need to garbage collection on a time where it won't affect the game.
I understand that you want to catch event about losing focus on app, to pause the game on whatever popup it may present, right?
So is the game still running in background or it pauses?
When the low battery popup occurs, the game behind is not suspended but keeps on playing. This is because Android system popups apparently do not interrupt or pause applications.
http://stackoverflow.com/questions/11361584/android-way-of-detecting-low-battery-notification
http://developer.android.com/training/monitoring-device-state/battery-monitoring.html
Monitoring battery status won't solve it because each user can set it's own limits when to present popup or disable it at all. So you can never be sure when popup will appear.
there must be a better way to detect when app loses focus, popup independent way
Do you have standard Android, or Mod?
http://stackoverflow.com/questions/7240916/android-under-what-circumstances-would-a-dialog-appearing-cause-onpause-to-be
I tested on the emulator too, normal Intel atom image of 4.2.2, and forced the battery to be 10% and it popped up without calling onPause.
Also, I do not think users can normally change this or set limit. The hard coded limit is when batter gets below <15% the system shows the popup as you can see by testing in the emulator.
http://www.stealthcopter.com/blog/2010/07/changing-the-battery-percentage-in-an-avd-android-virtual-device-emulator/
So, I added a broadcastreceiver and it successfully is detecting low battery state (the same that triggers the system dialog) and calls onPause.
In the main activity:<pre escaped='true' lang="java">
private BroadcastReceiver receiverBattery;
public void onCreate(Bundle savedInstanceState)
{
...
GiderosApplication.onCreate(externalClasses);
// LOW BATTERY START
receiverBattery = new BroadcastReceiver() {
@Override
public void onReceive(Context ctx, Intent intent) {
if (mPlaying == true)
{
GiderosApplication.getInstance().onPause();
mGLView.onPause();
mPlaying = false;
}
}
};
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_LOW);
registerReceiver(receiverBattery, filter);
// LOW BATTERY END
}
protected void onPause()
{
if (mPlaying == true)
{
GiderosApplication.getInstance().onPause();
mGLView.onPause();
mPlaying = false;
}
// LOW BATTERY START - Unregister in onPause to prevent memory leaks
try{
unregisterReceiver (receiverBattery);
}
catch (IllegalStateException e)
{
e.printStackTrace();
}
// LOW BATTERY END
super.onPause();
}
In my opinion, system level dialogs should have paused activity.
Ok, but how about other system dialogs? You can't really implement receiver for each and every case, I think there should be a more viable solution.
Maybe listening to onWindowFocusChanged?
EDIT: A side effect of this code is that any in-game created alert/dialog also fires OnPause, which is fine for me, but if people want the animation still running behind the dialog, they should probably comment out the mGLView.onPause() in this code.
Likes: ar2rsawseen