Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Redirect to App Settings

Discussion in 'Android' started by Qbit86, Mar 15, 2017.

  1. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    487
    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.
     
  2. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,663
    Qbit86 likes this.
  3. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    487
    Well, I've found Java code snippet:
    Code (csharp):
    1. String packageName = activity.getPackageName();
    2. Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.fromParts("package", packageName, null));
    3. intent.addCategory(Intent.CATEGORY_DEFAULT);
    4. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    5. activity.startActivity(intent);
    Translated it to Unity (constants substituted with literals):
    Code (csharp):
    1. try
    2. {
    3. #if UNITY_ANDROID
    4.     using (var unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    5.     using (AndroidJavaObject currentActivityObject = unityClass.GetStatic<AndroidJavaObject>("currentActivity"))
    6.     {
    7.         string packageName = currentActivityObject.Call<string>("getPackageName");
    8.  
    9.         using (var uriClass = new AndroidJavaClass("android.net.Uri"))
    10.         using (AndroidJavaObject uriObject = uriClass.CallStatic<AndroidJavaObject>("fromParts", "package", packageName, null))
    11.         using (var intentObject = new AndroidJavaObject("android.content.Intent", "android.settings.APPLICATION_DETAILS_SETTINGS", uriObject))
    12.         {
    13.             intentObject.Call<AndroidJavaObject>("addCategory", "android.intent.category.DEFAULT");
    14.             intentObject.Call<AndroidJavaObject>("setFlags", 0x10000000);
    15.             currentActivityObject.Call("startActivity", intentObject);
    16.         }
    17.     }
    18. #endif
    19. }
    20. catch (Exception ex)
    21. {
    22.     Debug.LogException(ex);
    23. }
    Seems to be working.
     
    Last edited: Mar 15, 2017
  4. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,663
  5. Yarden-Raveh

    Yarden-Raveh

    Joined:
    May 7, 2013
    Posts:
    4
    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

    <intent-filter>
    <action android:name="android.settings.APPLICATION_DETAILS_SETTINGS" />
    <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>

    to the com.unity3d.player.UnityPlayerActivity activity in the android manifest. didn't work.
     
    Last edited: Oct 2, 2017
  6. rogin0987

    rogin0987

    Joined:
    Feb 16, 2018
    Posts:
    3
    Hello sir! I Just saw your post about redirecting into App Settings. Can you please help me about that thing? :( I Cant find some of API for redirecting into Wifi Settings. i Hope you can help me for that thing :( Thanks a lot si
     
  7. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    487
    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.
     
  8. rogin0987

    rogin0987

    Joined:
    Feb 16, 2018
    Posts:
    3
    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<AndroidJavaObject>("currentActivity");
    var cn = currentActivity.Call<AndroidJavaObject>("ComponentName","com.android.settings","com.android.settings.wifi.WifiSettings");
    var intent = currentActivity.Call<AndroidJavaObject>("getIntent");


    intent.Call<AndroidJavaObject>("setComponent",cn);
    intent.Call<AndroidJavaObject>("setFlags", 0x10000000);
    intent.Call<AndroidJavaObject>("addCategory","android.intent.category.DEFAULT");
    currentActivity.Call<AndroidJavaObject>("startActivity", intent);
     
  9. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    487
    This constant Settings.ACTION_WIFI_SETTINGS is equal to string "android.settings.WIFI_SETTINGS", so we pass this value as literal:

    Code (csharp):
    1. using (var unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    2. using (AndroidJavaObject currentActivityObject =
    3.     unityClass.GetStatic<AndroidJavaObject>("currentActivity"))
    4. using (var intentObject = new AndroidJavaObject(
    5.     "android.content.Intent", "android.settings.WIFI_SETTINGS"))
    6. {
    7.     currentActivityObject.Call("startActivity", intentObject);
    8. }
     
    Last edited: Feb 19, 2018
    KBaker46 likes this.
  10. rogin0987

    rogin0987

    Joined:
    Feb 16, 2018
    Posts:
    3
    THANK YOU VERY MUCH SIR!! IT WORKS! IM DOING IT FOR 3 HOURS ALREADY T_T
     
    Qbit86 likes this.
  11. Shirzad

    Shirzad

    Joined:
    Dec 8, 2016
    Posts:
    13
    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?

     
  12. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    487
    What exactly have you tried? I've just tested, this approach works for me.
     
    Last edited: Mar 8, 2018
  13. rola12

    rola12

    Joined:
    Nov 8, 2016
    Posts:
    5
    Hi. I wanted to know how to open location services for an App in unity3d?
     
  14. rola12

    rola12

    Joined:
    Nov 8, 2016
    Posts:
    5
    android.settings.LOCATION_SOURCE_SETTINGS try this
     
  15. ShoaibAslamTintash

    ShoaibAslamTintash

    Joined:
    Sep 6, 2018
    Posts:
    11
    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"


    Code (CSharp):
    1. try
    2.         {
    3.             //Application.OpenURL(m_LicensingURL_Received);
    4.             using (var activity = m_Activity)
    5.             using (var uriClass = new AndroidJavaClass("android.net.Uri"))
    6.             using (var uri = uriClass.CallStatic<AndroidJavaObject>("parse", m_LicensingURL_Received))
    7.  
    8.             using (var paywallIntent = new AndroidJavaObject("android.intent.action.VIEW", uri))
    9.             using (paywallIntent.Call<AndroidJavaObject>("setPackage", "com.android.vending" /*Play Store */))
    10.             {
    11.                 activity.Call("startActivity", paywallIntent);
    12.             }
    13.         }
    14.         catch (Exception e)
    15.         {
    16.             Debug.LogError("Exception  "+ e);
    17.         }

    Can you guys help me with this?
     
    Last edited: Aug 8, 2020
  16. marc-call-healthscholars

    marc-call-healthscholars

    Joined:
    May 7, 2020
    Posts:
    2
    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?
     
  17. TheVirtualMunk

    TheVirtualMunk

    Joined:
    Sep 6, 2019
    Posts:
    143
    +1
     
  18. planetfactory

    planetfactory

    Joined:
    May 18, 2016
    Posts:
    55
    +1
     
  19. planetfactory

    planetfactory

    Joined:
    May 18, 2016
    Posts:
    55
    Try:

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

    Zaskar7777

    Joined:
    Oct 17, 2020
    Posts:
    36
    I tried the following to redirect the user to Android settings page, none of them worked what am I doing wrong?

    Code (CSharp):
    1.             using (var unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    2.             using (AndroidJavaObject currentActivityObject = unityClass.GetStatic<AndroidJavaObject>("currentActivity"))
    3.             {
    4.                 string packageName = currentActivityObject.Call<string>("getPackageName");
    5.  
    6.                 using (var uriClass = new AndroidJavaClass("android.net.Uri"))
    7.                 using (AndroidJavaObject uriObject = uriClass.CallStatic<AndroidJavaObject>("fromParts", "package", packageName, null))
    8.                 using (var intentObject = new AndroidJavaObject("android.content.Intent", "android.settings.WIFI_SETTINGS", uriObject))
    9.                 {
    10.                     intentObject.Call<AndroidJavaObject>("addCategory", "android.intent.category.DEFAULT");
    11.                     intentObject.Call<AndroidJavaObject>("setFlags", 0x100000[code=CSharp]using (var intentObject = new AndroidJavaObject("android.content.Intent", "android.settings.ACTION_LOCATION_SOURCE_SETTINGS", uriObject)));
    12.                     currentActivityObject.Call("startActivity", intentObject);
    13.                 }
    14.             }
    15. #endif
    16.         }
    17.         catch (Exception ex)
    18.         {
    19.             Debug.LogException(ex);
    20.         }
    I tried the following values for

    Code (CSharp):
    1.  
    2.                 using (var intentObject = new AndroidJavaObject("android.content.Intent", "android.settings.APPLICATION_DETAILS_SETTINGS", uriObject)) worked well
    3.  
    Code (CSharp):
    1. using (var intentObject = new AndroidJavaObject("android.content.Intent", "android.settings.LOCATION_SOURCE_SETTINGS", uriObject))
    2.  
    didnt work

    Code (CSharp):
    1. using (var intentObject = new AndroidJavaObject("android.content.Intent", "android.settings.ACTION_SETTINGS", uriObject))
    2.  
    3.  
    didnt work
     
  21. DineshMr_X

    DineshMr_X

    Joined:
    Aug 12, 2018
    Posts:
    1
    For accessing location settings of the current Activity try the following

    Code (CSharp):
    1.  try
    2.         {
    3. #if UNITY_ANDROID
    4.             using (var unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    5.             using (AndroidJavaObject currentActivityObject = unityClass.GetStatic<AndroidJavaObject>("currentActivity"))
    6.             {
    7.                 using (var intentObject = new AndroidJavaObject("android.content.Intent", "android.settings.LOCATION_SOURCE_SETTINGS"))
    8.                 {
    9.                     currentActivityObject.Call("startActivity", intentObject);
    10.                 }
    11.             }
    12. #endif
    13.         }
    14.         catch (Exception ex)
    15.         {
    16.             Debug.LogException(ex);
    17.         }
    18.  
    For accessing notification settings of the current Activity try the following

    Code (CSharp):
    1. try
    2.         {
    3. #if UNITY_ANDROID
    4.             using (var unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    5.             using (AndroidJavaObject currentActivityObject = unityClass.GetStatic<AndroidJavaObject>("currentActivity"))
    6.             {
    7.                 string packageName = currentActivityObject.Call<string>("getPackageName");
    8.              
    9.                  using (var intentObject = new AndroidJavaObject("android.content.Intent", "android.settings.APP_NOTIFICATION_SETTINGS"))
    10.                  {
    11.                      intentObject.Call<AndroidJavaObject>("putExtra", "android.provider.extra.APP_PACKAGE",packageName);
    12.                      currentActivityObject.Call("startActivity", intentObject);
    13.                  }
    14.             }
    15. #endif
    16.         }
    17.         catch (Exception ex)
    18.         {
    19.             Debug.LogException(ex);
    20.         }
    21.        
     
  22. Dev_BMAD

    Dev_BMAD

    Joined:
    Aug 8, 2023
    Posts:
    1
    Reference for the strings is available here:

    https://developer.android.com/reference/android/provider/Settings#ACTION_SETTINGS

    I was able to launch settings like this

    Code (CSharp):
    1.             using (var unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    2.             using (AndroidJavaObject currentActivityObject = unityClass.GetStatic<AndroidJavaObject>("currentActivity"))
    3.             {
    4.                 using (var intentObject = new AndroidJavaObject("android.content.Intent", "android.settings.SETTINGS"))
    5.                 {
    6.                     currentActivityObject.Call("startActivity", intentObject);
    7.                 }
    8.             }