Unity3D Android Lockscreen does not appear when the screen turns off. (Wakelock?)

I am building an Android app using Unity and it is going very well. However, I have a strange problem with the screen timeout, and the lock screen does not appear.

What is going to happen

  • User stops playing
  • Screen Timeout and Shutdown
  • Later, the player returns and returns his phone to
  • Lockscreen indicates whether the user can enter their password or otherwise unlock their phone.
  • The application restores focus and continues

What's happening

  • User stops playing
  • Screen Timeout and Shutdown
  • Later, the player returns and returns his phone to
  • Lockscreen does NOT show! The application is right in focus, bypassing all screen locks.
  • Users go crazy that their security is compromised :(

Notes

  • , Android
  • Unity 4.2.0f4 ( )
  • Android
  • 5 , .

, wakelock, Unity , . , lockscreen "load". .

- , ?

. Unity . , , .

+4
3

MichaelTaylor3D Answer , .

, , , , :

KeyguardManager . API 8 API 9+. API- Device Admin, .

UnityPlayer eclipse setWakeLock (boolean), .

Android. UnityPlayer setWakeLock(boolean) onPause.

, , , , , . Unity, , , .

public class UnityPlayerWithLockscreen extends UnityPlayerNativeActivity {


    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
         super.onCreate(savedInstanceState);
    }

    //Provides access to the private UnityPlayer.setWakeLock(boolean) method.
    public void setWakeLock(boolean useWakelock) {
        Method setWakeLockMethod;

        //Use Reflection to get the setWakeLock(boolean) method from the UnityPlayer class
        setWakeLockMethod = mUnityPlayer.getClass().getDeclaredMethod("setWakeLock");

        //Set the method to me accessible
        setWakeLockMethod.setAccessible(true);

        //Invoke the method with our argument
        setWakeLockMethod.invoke(mUnityPlayer, useWakelock);
    }


    @Override
    protected void onPause()
    {
        super.onPause();

        //Force unity to release the wakelock
        setWakeLock(false);     
    }
}

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".UnityPlayerWithLockscreen"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

- # # ↔ Java interop.

, , , API . , Unity .

+2

Unity, http://developer.android.com/reference/android/os/PowerManager.WakeLock.html

, - wakelock isHeld(); .

EDIT: wakelock , , , , , , , - , .

0

, ,

, Android SDK, Unity , .

Java- Plugins/Android.

Java : , .jar

package com.yourcompany.yourgamename;

import com.unity3d.player.UnityPlayerActivity;

import android.content.Context;
import android.os.Bundle;
import android.util.Config;
import android.util.Log;
import android.app.Activity;   

public class MainActivity extends UnityPlayerActivity 
{    
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
         super.onCreate(savedInstanceState);
    }

    public static void lockScreen()
    {
        KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE); 
        KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);  
        lock.reenableKeyguard();  
    }
}

, Android Manifest, Unity \Plugins\Android:

<?xml version="1.0" encoding="utf-8"?>

  

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".MainActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Unity # script, Java:

public static void InvokeAndroidLockScreen()
{
    #if UNITY_ANDROID
    using (AndroidJavaClass cls_UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    {
         using (AndroidJavaObject obj_Activity = cls_UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
         {
              obj_Activity .CallStatic("lockScreen");
         }
    }
    #else
    Debug.LogWarning("Cant invoke lockscreen on a non Android Device");
    #endif

}

, , lockscreen :

void OnApplicationPause(bool pauseStatus)
{
     InvokeAndroidLockScreen();
}

, false

Application.runInBackground = false;

, , . ( )

, , Unity, .

0

All Articles