Preventing from sleeping

Previous topic - Next topic

MrTAToad

Is it possible to modify the Android manifest file to prevent a program from sleeping, which apparently is currently done...

Kitty Hello

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.

MrTAToad

I've added android:keepScreenOn="true" to the manifest file, so I'll see if that works :)

Kitty Hello


MrTAToad

#4
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...

Kitty Hello

Seems to work .. as in .. really works?

MrTAToad

I'll be leaving it running for the next hour or so - and if the screen hasn't dimmed then it is working...

Kitty Hello

Great. I'll include that in V11 builds. Many thanks.

MrTAToad

No problem - it works well!

fivesprites

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