Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Android "Low Battery" popup does not dispatch APPLICATION_SUSPEND event — Gideros Forum

Android "Low Battery" popup does not dispatch APPLICATION_SUSPEND event

EricCarrEricCarr Member
edited October 2014 in General questions
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

  • Did you mean it doesnt fire MEMORY_WARNING?
    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.
  • @EricCarr I'm confused, do you mean low battery or low memory?

    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?
  • EricCarrEricCarr Member
    edited October 2014
    I'm sorry, I mis-typed. I meant to say "LOW BATTERY", not low memory. I've updated the subject and first sentence of my main post.

    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
  • Wow, now thats a drag

    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
  • For now I have not found any indication that something like that have been changed in Android KitKat, everywhere it still said that system popups will call onPause event of Activity

    Do you have standard Android, or Mod?
  • EricCarrEricCarr Member
    edited October 2014
    I have standard (not rooted) Android. According to this, a popup should not cause onPause to be called because it is technically not an activity:

    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();
    }
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    Hmm. That seems like a real overkill for a simple functionality that was working perfectly on previous versions.

    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?
  • EricCarrEricCarr Member
    edited October 2014
    You are right, I reverted the change and added the pause logic to onWindowFocusChanged and it is calling pause just fine with the low battery popup now. Thanks! :)

    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.
    <a href="http://forum.gideros.rocks/profile/Override" rel="nofollow">@Override</a>
    	public void onWindowFocusChanged(boolean hasFocus)
    	{
    		super.onWindowFocusChanged(hasFocus);
     
    		mHasFocus = hasFocus;
     
    		if (mHasFocus == true && mPlaying == false)
    		{
    			mGLView.onResume();
    			GiderosApplication.getInstance().onResume();
    			mPlaying = true;
    		}
     
                    // == NEW CODE ==
    		if (mHasFocus == false && mPlaying == true)
    		{
    			GiderosApplication.getInstance().onPause();
    			mGLView.onPause();
    			mPlaying = false;
    		}
                    // == END NEW CODE
    	}

    Likes: ar2rsawseen

    +1 -1 (+1 / -0 )Share on Facebook
  • EricCarrEricCarr Member
    edited October 2014
    (deleted - see edit above)
Sign In or Register to comment.