'How to launch Android Settings app to your app's settings with Xamarin.Essentials Launcher?

I am working on an app for Android and iOS using Xamarin.Forms along with Xamarin.Essentials. I'd like to be able to provide a button in my app that would take the user to the device's 'Settings' app, then into the specific settings page for this app I'm working on.

Previously, this has been done using a dependency service to call into platform-specific code. I believe one can accomplish this much more simply, and without any platform-specific code using the Xamarin.Essentials Launcher (Microsoft documentation here). I have figured out how to do this on iOS:

bool didOpenUri = await Xamarin.Essentials.Launcher.TryOpenAsync("app-settings:com.yourCompany.yourApp");

However, I have not been able to discover how to do this on Android. If anything is unclear, please let me know. Otherwise, any assistance is appreciated... Thanks very much!



Solution 1:[1]

@WendyZang very kindly provided the answer to the original question, which is that you unfortunately cannot launch the Android settings app (let alone drill into the settings specifically for your own app) using the Xamarin.Essentials Launcher.

Instead, you must use a Dependency Service with platform specific implementations. To help others who run into the same sort of issue, I wanted to add in a complete solution of setting up a Dependency Service on both iOS and Android, which navigates not only to the OS's settings app, but specifically into the settings for your app:

In your cross-platform project, the interface might look something like the following. You will need to pass in your app's bundle-id (eg, com.myCompany.myApp):

namespace MyCoolMobileApp.Services.DependencyServices
{
    public interface ISettingsAppLauncher
    {
        void LaunchSettingsApp(string appBundleId);
    }
}

In Android, your implementation could be like the following (note that I am using James Montemagno's CurrentActivity plugin):

using System.Diagnostics;
using Android.Content;
using Plugin.CurrentActivity; // https://github.com/jamesmontemagno/CurrentActivityPlugin
using MyCoolMobileApp.Droid.Services.DependencyServices;
using MyCoolMobileApp.Services.DependencyServices;
using Xamarin.Forms;

[assembly: Dependency(typeof(SettingsAppLauncher_Android))]
namespace MyCoolMobileApp.Droid.Services.DependencyServices
{
    public class SettingsAppLauncher_Android : ISettingsAppLauncher
    {
        public void LaunchSettingsApp(string  appBundleId)
        {
            var intent = new Intent(Android.Provider.Settings.ActionApplicationDetailsSettings);
            intent.AddFlags(ActivityFlags.NewTask);
            var uri = Android.Net.Uri.FromParts("package", appBundleId, null);
            intent.SetData(uri);
            CrossCurrentActivity.Current.AppContext.StartActivity(intent);
        }
    }
}

Last but not least, the iOS implementation would be:


using System.Diagnostics;
using Foundation;
using MyCoolMobileApp.iOS.Services.DependencyServices;
using MyCoolMobileApp.Services.DependencyServices;
using UIKit;
using Xamarin.Forms;

[assembly: Dependency(typeof(SettingsAppLauncher_iOS))]
namespace MyCoolMobileApp.iOS.Services.DependencyServices
{
    public class SettingsAppLauncher_iOS : ISettingsAppLauncher
    {
        public void LaunchSettingsApp(string appBundleId)
        {
            var url = new NSUrl($"app-settings:{appBundleId}");
            UIApplication.SharedApplication.OpenUrl(url);
        }
    }
}

Solution 2:[2]

https://github.com/xamarin/Essentials/pull/328

AppInfo.ShowSettingsUI();

This should open settings for iOS and Android using Xamarin Essentials

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jbachelor
Solution 2 jharrell