Two Issues With Android

Good morning Cocos2dx Community,

Yesterday I release my first game - Color-Shift onto the iOS app store. This was an update to the game that was previously done using cocos2d, and I got to say I really like using this framework. Now my android build is going well there were a few minor glitches happening that I had to resolve. I have two outstanding issues that I would like to fix before shipping the game to android. I will go ahead and state the issues and then give a bit of background on what I have found. First the game’s background music plays in the lock screen. Second the game does not pause when the user hits the multitask button.

So for the background music in the lock screen the way I reproduce this is I run my game hit the power button to sleep the device. Once the device is sleeped I press the power button again to wake it. Once awake I see the android lock screen and I can hear my game’s background music playing. Now in all other cases the background music does stop playing like for example if I minimize the app. I put some logs and found that onResume() is called once the the device wakes. Is there a way to check if the app has focus?

The next issue is that I am playing my game hit the multitask button. And I can see that the game is still running. Again I think that I can solve this if I can somehow listen to something to check if the app is still in focus. I am not sure because I am not an android expert.

Anyways any thoughts on these two problems. Hopefully someone that has ported their game over to android can give some advice. Also the device I am testing on is a Nextbook 7 running ice-cream sandwich but I also tested and confirmed that these issues happen on Samsung devices running Jelly bean.

Thanks!

Doing a little more investigation I found that in my game’s activity file I can override public void onWindowFocusChanged(boolean focus). This function onWindowFocusChanged is being called anytime there is a focus change in the app and it is properly reporting is the app has focus. Now I am unsure if this is something I should be touching or not and secondly where are the functions that are calling AppDelegate::applicationDidEnterBackground(), AppDelegate::applicationWillEnterForeground(). Looking around I cannot find the link from the java to the cpp for these two function. Again any help with this would be greatly appreciated!

Thanks and Happy Holidays!

Ok so I think I solved this issue but again I would like to hear some feedback from any android experts in the community. Pretty much I had to make a few changes inside the Cocos2dxActivity.java. Here are the changes:

       @Override
    protected void onResume() {
        super.onResume();
        Cocos2dxHelper.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();

        Cocos2dxHelper.onPause();

    }

    @Override
    public void onWindowFocusChanged(boolean focus){
        if(focus){
            this.mGLSurfaceView.onResume();
        }
        else
        {
            this.mGLSurfaceView.onPause();
        }
    }

So as you can see the onResume and onPause no longer execute this.mGLSurfaceView.onResume(); and this.mGLSurfaceView.onPause();, this all happens inside the onWindowFocusChanges() function. This makes sure that as soon as we loose focus of the app we handle a pause and we only resume the app once we got focus again. Testing I can confirm that this will turn off the background music in my game and additionally it will pause my game and turn the background music off when I bring up the andriod multitask. Is there any other interrupts that I should test for? Any comments?

Thanks

This is what google said about this:

The second version similar (but not the same) as your version:

  1. Pause playback when you receive onPause().

  2. When you receive onResume():

If you have previously received an onFocusChanged(false) message, wait for an onFocusChanged(true) message to arrive before resuming playback.

If you have not previously received an onFocusChanged(false) message, then resume audio immediately.

Jakub L wrote:

Ok so I think I solved this issue but again I would like to hear some feedback from any android experts in the community. Pretty much I had to make a few changes inside the Cocos2dxActivity.java. Here are the changes:
>
[…]
>
So as you can see the onResume and onPause no longer execute this.mGLSurfaceView.onResume(); and this.mGLSurfaceView.onPause();, this all happens inside the onWindowFocusChanges() function. This makes sure that as soon as we loose focus of the app we handle a pause and we only resume the app once we got focus again. Testing I can confirm that this will turn off the background music in my game and additionally it will pause my game and turn the background music off when I bring up the andriod multitask. Is there any other interrupts that I should test for? Any comments?
>
Thanks