Redirect to App Settings

How to redirect user from Unity game to system settings for this application, namely permission settings?

Starting from Android 6.0 Marshmallow (API level 23) developer should provide standard flow for asking dangerous permissions in run time. In case of “Don’t ask again” option has been selected, guideline suggests redirecting user to standard settings.

Probably not the answer your looking for, but I think android goodies pro provides the ability to open app settings:

Well, I’ve found Java code snippet:

String packageName = activity.getPackageName();
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.fromParts("package", packageName, null));
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(intent);

Translated it to Unity (constants substituted with literals):

try
{
#if UNITY_ANDROID
    using (var unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    using (AndroidJavaObject currentActivityObject = unityClass.GetStatic<AndroidJavaObject>("currentActivity"))
    {
        string packageName = currentActivityObject.Call<string>("getPackageName");

        using (var uriClass = new AndroidJavaClass("android.net.Uri"))
        using (AndroidJavaObject uriObject = uriClass.CallStatic<AndroidJavaObject>("fromParts", "package", packageName, null))
        using (var intentObject = new AndroidJavaObject("android.content.Intent", "android.settings.APPLICATION_DETAILS_SETTINGS", uriObject))
        {
            intentObject.Call<AndroidJavaObject>("addCategory", "android.intent.category.DEFAULT");
            intentObject.Call<AndroidJavaObject>("setFlags", 0x10000000);
            currentActivityObject.Call("startActivity", intentObject);
        }
    }
#endif
}
catch (Exception ex)
{
    Debug.LogException(ex);
}

Seems to be working.

Nice!

Hi,
I’ve been trying to use this in my app and although it works(the user is redirected to app settings) the application immediately crashes(with the message “Unfortunately, the app has stopped”), any ideas why it could be? Maybe i need to declare the intent in the manifest first?

i tried adding this intent

to the com.unity3d.player.UnityPlayerActivity activity in the android manifest. didn’t work.

Hello sir! I Just saw your post about redirecting into App Settings. Can you please help me about that thing? :frowning: I Cant find some of API for redirecting into Wifi Settings. i Hope you can help me for that thing :frowning: Thanks a lot si

Sure: https://www.google.ru/search?q=android+redirect+to+wi-fi+settings

First answer suggests this variant: startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
Probably it is worth trying to translate from Java to Unity proxies.

I Tried it Already sir. But When i run the app nothing happens sir. This is my code

var UnityPlayer = new AndroidJavaClass(“com.unity3d.player.UnityPlayer”);
var currentActivity = UnityPlayer.GetStatic(“currentActivity”);
var cn = currentActivity.Call(“ComponentName”,“com.android.settings”,“com.android.settings.wifi.WifiSettings”);
var intent = currentActivity.Call(“getIntent”);

intent.Call(“setComponent”,cn);
intent.Call(“setFlags”, 0x10000000);
intent.Call(“addCategory”,“android.intent.category.DEFAULT”);
currentActivity.Call(“startActivity”, intent);

This constant Settings.ACTION_WIFI_SETTINGS is equal to string “android.settings.WIFI_SETTINGS”, so we pass this value as literal:

using (var unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
using (AndroidJavaObject currentActivityObject =
    unityClass.GetStatic<AndroidJavaObject>("currentActivity"))
using (var intentObject = new AndroidJavaObject(
    "android.content.Intent", "android.settings.WIFI_SETTINGS"))
{
    currentActivityObject.Call("startActivity", intentObject);
}

THANK YOU VERY MUCH SIR!! IT WORKS! IM DOING IT FOR 3 HOURS ALREADY T_T

Dear Qbit82,

i try this with “ACTION_LOCATION_SOURCE_SETTINGS” to enable GPS on runtime. But this is not working. Sir can you help me please?

What exactly have you tried? I’ve just tested, this approach works for me.

Hi. I wanted to know how to open location services for an App in unity3d?

android.settings.LOCATION_SOURCE_SETTINGS try this

I am trying to start google play paywall from unity app and it is giving me an error that “Java.lang.ClassNotFoundException: android.intent.action.VIEW”

try
        {
            //Application.OpenURL(m_LicensingURL_Received);
            using (var activity = m_Activity)
            using (var uriClass = new AndroidJavaClass("android.net.Uri"))
            using (var uri = uriClass.CallStatic<AndroidJavaObject>("parse", m_LicensingURL_Received))

            using (var paywallIntent = new AndroidJavaObject("android.intent.action.VIEW", uri))
            using (paywallIntent.Call<AndroidJavaObject>("setPackage", "com.android.vending" /*Play Store */))
            {
                activity.Call("startActivity", paywallIntent);
            }
        }
        catch (Exception e)
        {
            Debug.LogError("Exception  "+ e);
        }

Can you guys help me with this?

Hey guys,
So the above code had been working for me on the Quest 1 headset (Android 7), but it no longer works on the Quest 2 headset (Android 10).

I’ve been searching for a week now trying to find an answer about why this is broken and how to correctly pull up the app settings for the user… Does anyone have any idea on what needs to happen for this to work for Android v10?

+1

+1

Try:

try {
using (var unityPlayer = new AndroidJavaClass(“com.unity3d.player.UnityPlayer”))
using (var activity = unityPlayer.GetStatic(“currentActivity”))
using (var uriClass = new AndroidJavaClass(“android.net.Uri”))
using (var uri = uriClass.CallStatic(“parse”, m_LicensingURL_Received))
using (var paywallIntent = new AndroidJavaObject(“android.content.Intent”, “android.intent.action.VIEW”, uri))
using (paywallIntent.Call(“setPackage”, “com.android.vending”)) {
activity.Call(“startActivity”, paywallIntent);
}
} catch (Exception e) {
Debug.LogError(e.ToString());
}

I tried the following to redirect the user to Android settings page, none of them worked what am I doing wrong?

            using (var unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
            using (AndroidJavaObject currentActivityObject = unityClass.GetStatic<AndroidJavaObject>("currentActivity"))
            {
                string packageName = currentActivityObject.Call<string>("getPackageName");

                using (var uriClass = new AndroidJavaClass("android.net.Uri"))
                using (AndroidJavaObject uriObject = uriClass.CallStatic<AndroidJavaObject>("fromParts", "package", packageName, null))
                using (var intentObject = new AndroidJavaObject("android.content.Intent", "android.settings.WIFI_SETTINGS", uriObject))
                {
                    intentObject.Call<AndroidJavaObject>("addCategory", "android.intent.category.DEFAULT");
                    intentObject.Call<AndroidJavaObject>("setFlags", 0x100000[code=CSharp]using (var intentObject = new AndroidJavaObject("android.content.Intent", "android.settings.ACTION_LOCATION_SOURCE_SETTINGS", uriObject)));
                    currentActivityObject.Call("startActivity", intentObject);
                }
            }
#endif
        }
        catch (Exception ex)
        {
            Debug.LogException(ex);
        }

I tried the following values for

                using (var intentObject = new AndroidJavaObject("android.content.Intent", "android.settings.APPLICATION_DETAILS_SETTINGS", uriObject)) worked well
using (var intentObject = new AndroidJavaObject("android.content.Intent", "android.settings.LOCATION_SOURCE_SETTINGS", uriObject))

didnt work

using (var intentObject = new AndroidJavaObject("android.content.Intent", "android.settings.ACTION_SETTINGS", uriObject))

didnt work