Skip to main content
Known Participant
January 15, 2013
Answered

Prevent screen from going to sleep

  • January 15, 2013
  • 2 replies
  • 11185 views

How to prevent mobile device (Android or iOS) to put screen to sleep?

I'm using Flash CS6 + AIR, in publish settings under Permissions I do have these two marked: DISABLE_KEYGUARD and WAKE_LOCK. But I don't see it working, i'm testing on Samsung Galaxy S3 (latest Android OS).

Although it says it only needs to be in XML file those two lines, don't I have to put anything in ActionScript 3.0 code inside my Flash document ?

When I pack APK file is XML also packed there ?

Thanks

This topic has been closed for replies.
Correct answer Colin Holgate

'desktop' is just the package that those functions and constants live in, they still apply to mobile.

If you are putting your code in the timeline you could literally copy and paste what I wrote into frame 1, just delete the two 'private' words.

Permissions in Android just states that at some point your app may use those features. The user has to agree to letting you do that, otherwise the APK won't install. That doesn't mean that you'll use those abilities right away, but when you do use the features from AS3, then the user has already allowed your app to do so.

The deactivate part is important. Your app would get rejected by Amazon if a user runs your app and then finds that their device doesn't sleep when your app is in the background.

2 replies

Inspiring
June 1, 2018

Hi Colin:

I followed your solution and it works fine for my apps with no ads but on those where I use the milkman games ANE for admob this solution stops working after calling the first interstitial.

I call interstitials by putting this code on the swfs where I want them to show:

import com.milkmangames.nativeextensions.*;

import com.milkmangames.nativeextensions.events.*;

if(AdMob.isInterstitialReady())

{

        AdMob.showPendingInterstitial();

}

For some reason calling the interstitials “cancels”  the code to prevent screen from going to sleep

Any advice on this?

Best Regards,

Colin Holgate
Inspiring
June 1, 2018

Don't you get any events from the ad to say that it's finished? If you don't, how do you know to return to your game?

Assuming you do, just put the code into that event listener as well. NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.KEEP_AWAKE; can be done any time, not just on activate.

Worst case, if you get no events at all, you could set a timer when the add fires, that is longer than the ad, and hopefully shorter than the user's screen saver, then set the idle mode when the timer fires.

Or just keep setting it all the time.

Inspiring
June 1, 2018

Hi again Colin:

I am not good at all on AS3.

I use this code at the very first frame of my app to integrate milkman games ANE = banner and interstitials:

import com.milkmangames.nativeextensions.*;

import com.milkmangames.nativeextensions.events.*;

if(AdMob.isSupported)

  {

AdMob.init(“—“);

}

else

{

trace("AdMob won't work on this platform!");

return;

}

AdMob.showAd(AdMobAdType.SMART_BANNER,AdMobAlignment.CENTER,AdMobAlignment.TOP);

AdMob.addEventListener(AdMobErrorEvent.FAILED_TO_RECEIVE_AD,onFailedReceiveAd);

AdMob.addEventListener(AdMobEvent.RECEIVED_AD,onReceiveAd);

AdMob.addEventListener(AdMobEvent.SCREEN_PRESENTED,onScreenPresented);

AdMob.addEventListener(AdMobEvent.SCREEN_DISMISSED,onScreenDismissed);

AdMob.addEventListener(AdMobEvent.LEAVE_APPLICATION,onLeaveApplication);

/** Try to load next interstitial */

function tryLoadNextInterstitial():void

{

    try

    {

        AdMob.loadInterstitial(“—“, false);

    } catch (e:Error) {

        trace("can't load next ad yet.");

    }

}

/** On Failed Receive Ad */

function onFailedReceiveAd(e:AdMobErrorEvent):void

{

trace("Ad failed to load.");

trace("ERROR receiving ad, reason: '"+e.text+"'");

        if(e.isInterstitial) tryLoadNextInterstitial();

}

/** On Receive Ad */

function onReceiveAd(e:AdMobEvent):void

{

  trace("Received ad:"+e.isInterstitial+":"+e.dimensions);

}

/** On Screen Presented */

function onScreenPresented(e:AdMobEvent):void

{

  trace("Screen Presented.");

}

/** On Screen Dismissed */

function onScreenDismissed(e:AdMobEvent):void

{

  trace("Screen Dismissed.");

        if(e.isInterstitial) tryLoadNextInterstitial();

}

/** On Leave Application */

function onLeaveApplication(e:AdMobEvent):void

{

  trace("Leave Application.");

        if(e.isInterstitial) tryLoadNextInterstitial();

}

tryLoadNextInterstitial();

Then on each swf I want to show the interstitial I use this code as advised from milkman games:

import com.milkmangames.nativeextensions.*;

import com.milkmangames.nativeextensions.events.*;

if(AdMob.isInterstitialReady())

{

        AdMob.showPendingInterstitial();

}

Sorry but that’s all I know. I can’t say if I get any events from the ad to say that it's finished.

Best Regards

Colin Holgate
Inspiring
January 15, 2013

These functions will give you an idea of what you have to do on the AS3 side:

import flash.desktop.NativeApplication;

import flash.desktop.SystemIdleMode;

stage.addEventListener(Event.ACTIVATE, fl_Activate);

stage.addEventListener(Event.DEACTIVATE, fl_Deactivate);

private function fl_Activate(event:Event=null):void {

NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.KEEP_AWAKE;

}

private function fl_Deactivate(event:Event):void {

NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.NORMAL;

}

The XML, where you have set the wake lock permissions for Android, does end up in the APK.

mari8899Author
Known Participant
January 15, 2013

Why 'desktop' , I'm doing mobile app only, but I don't know AS3.0 that much so...

Why there are such permissions for AIR deployment if they don't work? I mean if you have to add aditional AS3 code?

Can you help me and tell me what exactly code I need to put and where, on first frame or ?

I just need App to be always ON, so that screen doesn't go off.

Thanks !

Colin Holgate
Colin HolgateCorrect answer
Inspiring
January 15, 2013

'desktop' is just the package that those functions and constants live in, they still apply to mobile.

If you are putting your code in the timeline you could literally copy and paste what I wrote into frame 1, just delete the two 'private' words.

Permissions in Android just states that at some point your app may use those features. The user has to agree to letting you do that, otherwise the APK won't install. That doesn't mean that you'll use those abilities right away, but when you do use the features from AS3, then the user has already allowed your app to do so.

The deactivate part is important. Your app would get rejected by Amazon if a user runs your app and then finds that their device doesn't sleep when your app is in the background.