[Android] How to dim screen without locking device?

Hi!

I keep finding how to prevent device from locking (if that is what led You to this Answer its Screen.sleepTimeout = SleepTimeout.NeverSleep;).

In fact its part of solution to my problem. What I aim to achive is to keep device from locking but dim screen/backlight to save battery. Application should stay responsive underneath so that user can bring it up with one tap.

Thanks in advance!

Here is how I solved this:

It’s based on an answer by Khalos … and before that MichaelTaylor3D

It accesses the Android wakelock for your Unity project, which has 4 different levels to choose from: PARTIAL_WAKE_LOCK, SCREEN_DIM_WAKE_LOCK, SCREEN_BRIGHT_WAKE_LOCK, FULL_WAKE_LOCK

1. Build this java plugin below and put in your .jar file in a subdirectory named “Assets\Plugins\Android”.
(for help on building a .jar here are some instructions)


package com.YourCompany.YourAppName;

import com.unity3d.player.UnityPlayerNativeActivity;

import android.content.Context;
import android.util.Log;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;

public class UnityPlayerWithWakeLock extends UnityPlayerNativeActivity
{
	private PowerManager.WakeLock wl;

    //protected void onCreate(Bundle savedInstanceState)
    @Override
    protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);

		int wakeLock = PowerManager.SCREEN_DIM_WAKE_LOCK; // .. or PARTIAL_WAKE_LOCK or SCREEN_DIM_WAKE_LOCK or SCREEN_BRIGHT_WAKE_LOCK or FULL_WAKE_LOCK
		Log.i("Unity", "Setting wake lock to " + Integer.toString(wakeLock) );

		PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
		wl = pm.newWakeLock(wakeLock, "UnityPlayerWithWakeLock");
    }

	@Override
	protected void onPause()
	{
		super.onPause();
		wl.release();
	}

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

**2.** You need to [edit your AndroidManifest.xml](http://forum.unity3d.com/threads/modifying-android-manifest.113094/) file. Once in there, just change this

``` android:name="com.unity3d.player.UnityPlayerActivity" ```

.. to this .

android:name=".UnityPlayerWithWakeLock"

… and also add this

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

^(thanks to Obsurveyor for noting that one)


You don’t need to add any script hooks, and it should all just work.

Also here’s the batch file I last used to build the .jar files… i’ve called it BuildJava.bat and run it from Assets/Plugins/Android


"C:\Program Files\Java\jdk1.7.0_79\bin\javac.exe" UnityPlayerWithWakeLock.java -classpath "C:\Program Files\Unity\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Release\Classes\classes.jar" -bootclasspath C:\Users\[YOUR USER]\AppData\Local\Android\sdk\platforms\android-23\android.jar -d . -Xlint:deprecation

pause

"C:\Program Files\Java\jdk1.7.0_79\bin\javap.exe" -s com.[YOUR PACKAGE NAME HERE].UnityPlayerWithWakeLock

pause

"C:\Program Files\Java\jdk1.7.0_79\bin\jar.exe" cvfM ../UnityPlayerWithWakeLock.jar com/

pause

I would love an answer on this. Would be great for games that need to stay active and rely on audio cues for the user to do things in the real world, while the device remains dimmed without eventually shutting off.

Hi, sorry to de-necrotize this thread but this is for anyone who is still looking for a solution.

I recently had the same problem and despite quite a lot of googling, there didn’t seem to be any answers. So I built a tool in Unity to do it, it’s called Dimmer, and I’ve made it available on the asset store: Dimmer | Integration | Unity Asset Store

To darken the screen and in combination with Screen.sleepTimeout you can use Dimmer like this:

Screen.sleepTimeout = SleepTimeout.NeverSleep;
dimmer.brightness = 0.1f;

So it’s pretty easy to use, it works on iOS and Android and it doesn’t require any android permissions to work. Hope someone finds it helpful.