How to make android sleep?



  • Does anyone know how to make android sleep using Haxeflixel/openfl?

    I successfully removed wake-lock from the AndroidManifest.xml permissions but it didn't solve the issue. The game will stay on no matter what.

    I also tried using a wake lock extension but it didn't work either. (https://github.com/HaxeExtension/extension-wake-lock)

    Any help is appreciated thanks!



  • @flood

    Which AndroidManifest.xml file did you edit? If you edit the one in you export/bin it won't work because that one is generated when the project is compiled. You need to make a template file and add a node in project.xml to tell the compiler to use that one. I did this a while ago, so I hope I remember.

    I made a copy of C:\HaxeToolkit\haxe\lib\lime\2,9,1\templates\android\template\AndroidManifest.xml (or similar, depending on your install location)
    This is a copy of original:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="::APP_PACKAGE::" android:versionCode="::APP_BUILD_NUMBER::" android:versionName="::APP_VERSION::" android:installLocation="::ANDROID_INSTALL_LOCATION::">
    	
    	<uses-feature android:glEsVersion="0x00020000" android:required="true" />
    	::if (ANDROID_PERMISSIONS != null)::::foreach ANDROID_PERMISSIONS::
    	<uses-permission android:name="::__current__::" />::end::::end::
    	
    	<uses-sdk android:minSdkVersion="::ANDROID_MINIMUM_SDK_VERSION::" android:targetSdkVersion="::ANDROID_TARGET_SDK_VERSION::"/>
    	
    	<application android:label="::APP_TITLE::" android:debuggable="::DEBUG::"::if (HAS_ICON):: android:icon="@drawable/icon"::end:: android:allowBackup="true" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:hardwareAccelerated="true">
    		
    		<activity android:name="MainActivity" android:launchMode="singleTask" android:label="::APP_TITLE::" android:configChanges="keyboardHidden|orientation|screenSize|screenLayout"::if (WIN_ORIENTATION=="portrait"):: android:screenOrientation="sensorPortrait"::end::::if (WIN_ORIENTATION=="landscape"):: android:screenOrientation="sensorLandscape"::end::>
    			
    			<intent-filter>
    				
    				<action android:name="android.intent.action.MAIN" />
    				<category android:name="android.intent.category.LAUNCHER" />
    				<category android:name="tv.ouya.intent.category.GAME" />
    				
    			</intent-filter>
    			
    		</activity>
    		
    	</application>
    	
    </manifest>
    

    By default I think there are four permissions allowed, but I don't remember what they are. I only wanted
    INTERNET and ACCESS_NETWORK_STATE so I removed the part that allowed the defaults and added these two to my template.

    This is what my template looks like:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="::APP_PACKAGE::" android:versionCode="::APP_BUILD_NUMBER::" android:versionName="::APP_VERSION::" android:installLocation="::ANDROID_INSTALL_LOCATION::">
    		
    	::if WIN_REQUIRE_SHADERS::<uses-feature android:glEsVersion="0x00020000" android:required="true" />::elseif WIN_ALLOW_SHADERS::<uses-feature android:glEsVersion="0x00020000" android:required="false" />::end::
    	
    	<!--Include required permissions for Google Mobile Ads to run-->
    	<uses-permission android:name="android.permission.INTERNET" />
    	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    	
    	<uses-sdk android:minSdkVersion="::ANDROID_MINIMUM_SDK_VERSION::" android:targetSdkVersion="::ANDROID_TARGET_SDK_VERSION::"/>
    	
    	<application android:label="::APP_TITLE::" android:debuggable="::DEBUG::"::if (HAS_ICON):: android:icon="@drawable/icon"::end:: android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:hardwareAccelerated="true">
    		
    		<activity android:name="MainActivity" android:launchMode="singleTask" android:label="::APP_TITLE::" android:configChanges="keyboardHidden|orientation|screenSize|screenLayout"::if (WIN_ORIENTATION=="portrait"):: android:screenOrientation="sensorPortrait"::end::::if (WIN_ORIENTATION=="landscape"):: android:screenOrientation="sensorLandscape"::end::>
    			
    			<intent-filter>
    				
    				<action android:name="android.intent.action.MAIN" />
    				<category android:name="android.intent.category.LAUNCHER" />
    				<category android:name="tv.ouya.intent.category.GAME" />
    				
    			</intent-filter>
    			
    		</activity>
    		
    	</application>
    	
    	<supports-screens 
    		android:anyDensity="true" 
    		android:smallScreens="true" 
    		android:normalScreens="true" 
    		android:largeScreens="true" 
    		android:xlargeScreens="true" />
    	
    </manifest>
    

    I save this template in the folder named templates, above my projects folder so I can use it for all projects. So in project.xml I add this node:

    <template if="android" path="../templates/AndroidManifest.xml" rename="AndroidManifest.xml" />
    

    Change the path if you put it somewhere else.

    If you want to disallow the internet/network just remove those lines in the template.

    I hope I remembered this and explained it ok.



  • Thanks for the reply dean

    Yes I originally did that by following these guides and I removed the vibration and wake lock permissions (just like you :D):
    http://www.steverichey.com/dev/openfl-android-tips-and-tricks/ https://player03.com/2014/08/14/template-basics/

    The wake lock permission does not show up when I look at the android app info but the wake lock behavior is still present. Wake lock is killing the battery super fast so I'm trying to trigger sleeping to save battery power.

    0_1521402671182_Screenshot_2018-03-18-15-42-00.png



  • @flood

    ah, ok.

    My apps go to sleep after no activity. Just a guess, maybe you have something running in the background on a separate thread that keeps it active?



  • I found the problem and it was hardware related.

    I had the sleep timer on the phone set to 30 minutes but after changing it to 15 seconds it sleeps now. (I'm a noob -_-)

    Thanks for the help!



  • @flood

    yw. Glad you found the problem.


Log in to reply
 

Looks like your connection to HaxeFlixel was lost, please wait while we try to reconnect.