GLBasic forum

Main forum => GLBasic - en => Topic started by: MrTAToad on 2012-Apr-22

Title: Preventing from sleeping
Post by: MrTAToad on 2012-Apr-22
Is it possible to modify the Android manifest file to prevent a program from sleeping, which apparently is currently done...
Title: Re: Preventing from sleeping
Post by: Kitty Hello on 2012-Apr-24
http://stackoverflow.com/questions/2039555/how-to-get-an-android-wakelock-to-work

I'll add this to V11. But if you manage to make it for V10, ppl will not be sad.
Title: Re: Preventing from sleeping
Post by: MrTAToad on 2012-Apr-24
I've added android:keepScreenOn="true" to the manifest file, so I'll see if that works :)
Title: Re: Preventing from sleeping
Post by: Kitty Hello on 2012-Apr-24
<drumroll>...
Title: Re: Preventing from sleeping
Post by: MrTAToad on 2012-Apr-24
No, didn't work unfortunately - the screen still went dark and then the device still was able to suspend.

Need to try a few other things now :)

Any idea where
Code (glbasic) Select
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); could go ?

Think I found the place :)

This seems to work :

Code (glbasic) Select
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
                             WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);


although the first code seems to also work fine...
Title: Re: Preventing from sleeping
Post by: Kitty Hello on 2012-Apr-24
Seems to work .. as in .. really works?
Title: Re: Preventing from sleeping
Post by: MrTAToad on 2012-Apr-24
I'll be leaving it running for the next hour or so - and if the screen hasn't dimmed then it is working...
Title: Re: Preventing from sleeping
Post by: Kitty Hello on 2012-Apr-24
Great. I'll include that in V11 builds. Many thanks.
Title: Re: Preventing from sleeping
Post by: MrTAToad on 2012-Apr-24
No problem - it works well!
Title: Re: Preventing from sleeping
Post by: fivesprites on 2012-Sep-06
Slightly old topic, but I thought it worth updating rather than creating a new one.

The above changes didn't work for me on various devices and I had to perform a few other changes to get it to work properly.

In summary, I had to employ the use of "wakelocks" by updating the AndroidManifest.xml and also updating SDLActivity.java.  This allows us to enforce a full wake lock which prevents dimming and the eventual stand-by action.

I've already passed these changes on to Gernot for possible inclusion in a future GLB release.  (Hi Gernot - krakatomato here!) :)

I haven't attached my SDLActivity.java file as I'm unsure as to the license implications (GLB free vs GLB pro) however, the changes are simple to include:

In AndroidManifest.xml, add the new permission:

<uses-permission android:name="android.permission.WAKE_LOCK"

e.g.:

Code (glbasic) Select

    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.BATTERY_STATS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<supports-screens android:resizeable="false"


In SDLActivity.java we need to make a few changes:

Add the following at the top of the file (anywhere after the class definition, but before the onCreate() method):

Code (glbasic) Select

    // Power management
    PowerManager pm;
    PowerManager.WakeLock wl;


e.g.:

Code (glbasic) Select

    // Audio
    private static Thread mAudioThread;
    private static AudioTrack mAudioTrack;

    // Power management
    PowerManager pm; // <<-- ADD THIS LINE
    PowerManager.WakeLock wl; // <<-- ADD THIS LINE

    // Load the .so
    static {
        System.loadLibrary("SDL");
        System.loadLibrary("SDL_image");
        System.loadLibrary("mikmod");
        System.loadLibrary("SDL_mixer");
        System.loadLibrary("SDL_ttf");
        System.loadLibrary("main");
    }


Within the onCreate() method we also need to ensure that FLAG_KEEP_SCREEN_ON is set (as mentioned in this thread):

Code (glbasic) Select

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                             WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);


We also need to add the following to the very end of the onCreate() function:

Code (glbasic) Select

pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "GLBTag");
wl.acquire();


And finally, we need to ensure we release/acquire the wakelock on certain events:

Within onPause(), add:

Code (glbasic) Select

    wl.release();


Within onResume(), add:

Code (glbasic) Select

    wl.acquire();


and within onStop(), add:

Code (glbasic) Select

    wl.release();


Don't think I've missed anything, and I apologise if this sounds complicated - it's not really - and it certainly works :)

//Andy