Search code examples
androidunity-game-engineandroid-permissionsvirtual-reality

How to setup an Android intent in Unity3D to navigate from my application, to settings, and back again


I have a Unity3D VR application (platform agnostic) that needs to navigate from the app to settings and back. What we are doing is trying to install an APK from within our VR application. In this process, the android OS must confirm with the user if that's ok before proceeding. We would like to hook into this permissions process to be able to call it at a different stage in the application, and if possible listen to when the user has confirmed their choice, and then react in the app accordingly.

So far, I can use this code to force my app to check the permissions for installing from unknown sources, and then send you to the correct settings. Note this code is far from cleaned up, but you get the general idea:

var unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
var currentActivity = unityClass.GetStatic<AndroidJavaObject>("currentActivity");
var packageManager = currentActivity.Call<AndroidJavaObject>("getPackageManager");
bool launchIntent = packageManager.Call<bool>("canRequestPackageInstalls");

if (!launchIntent)
{
    try
    {
        using AndroidJavaClass unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        using AndroidJavaObject currentActivity = unityClass.GetStatic<AndroidJavaObject>("currentActivity");
        string packageName = currentActivity.Call<string>("getPackageName");
        using AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri");
        using AndroidJavaObject uriObject =
                uriClass.CallStatic<AndroidJavaObject>("fromParts", "package", packageName, null);
        using AndroidJavaObject intentObj = new AndroidJavaObject("android.content.Intent",
                "android.settings.MANAGE_UNKNOWN_APP_SOURCES", uriObject);
            
        currentActivity.Call("startActivity", intentObj);
    }
    catch (Exception ex)
    {
        Debug.LogException(ex);
    }
}

What I would like to do is

  1. Install our VR app
  2. on First run it will check these permissions
  3. if they are not granted, inform the user, then navigate the user to the correct settings window to 'enable' it.
  4. In our VR app, listen for this change and act accordingly. OR
  5. What intent flags should I have to ensure the back to application button/back arrow is shown in the settings window? On a different device the same code shows an arrow box at the bottom and I can click it and it takes me back to the application and continues from where I left off. How do I make sure that shows for every device?

EDIT 4/11/22: If I can ensure that button comes up then I can just check back in the app again if the settings have been enabled and everything is good.


Solution

  • For now, the code I posted runs differently on 2 diff VR headsets. One will send us back to the app, the other does not have a back button. For now we have a solution for both issues.