2

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!

jbachelor
  • 95
  • 2
  • 7

3 Answers3

3

As I know, there are no URLs for Android Settings. The Launcher do not support it.

But, the Settings class provides action strings for the Settings app.

Settings Class: https://learn.microsoft.com/en-us/dotnet/api/android.provider.settings?view=xamarin-android-sdk-9

You could do this in Android:

StartActivity(new Intent(Settings.ActionSettings));

And use Dependency service to do that in Xamarin.Forms.

Dependency service: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/introduction

Updated:

In Xamarin.Forms:

Create a interface:

 public interface IStartSetttings
 {
    void StartSettings();
 }

Implementation of Android:

[assembly: Dependency(typeof(StartSettings_Implementation))]
namespace XamarinDemo.Droid.DependencyService_Implementation
{
    class StartSettings_Implementation : MainActivity, IStartSetttings
    {
        public void StartSettings()
        {
            var intent = new Intent(Settings.ActionSettings);
            Forms.Context.StartActivity(intent);          

        }
    }
}

Usage in Xamarin.Forms:

 DependencyService.Get<IStartSetttings>().StartSettings();

Screenshot:

enter image description here

Wendy Zang - MSFT
  • 10,509
  • 1
  • 7
  • 17
  • Thanks very much for your help, @WendyZang. Good to know that it's not supported. I was not able to use the code snippet you provided (not quite enough detail for me to understand how/where to use it), but I do appreciate it! – jbachelor Apr 17 '20 at 17:30
  • I have uploaded all the code about how to start settings in Xamarin.forms via dependency service. Please check it. – Wendy Zang - MSFT Apr 21 '20 at 02:16
  • Thanks, @WendyZang... Greatly appreciated! I'm going to mark this as the answer, since the original question was how to open up your app's system settings from within your own app using Xamarin.Essentials, and you've already answered that: you can't, so you have to use a DependencyService with platform specific implementations. – jbachelor Apr 22 '20 at 15:40
2

@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);
        }
    }
}
jbachelor
  • 95
  • 2
  • 7
  • Thanks for the solution and I have a question. Your code works well in simulator, but when I deploy an ad hoc version to my iphone7, it just opens the general settings page. And I even didn't see my app on the Settings page. Did you meet something similar before? – Vanderwood Nov 14 '20 at 04:03
  • Hi @Serena - Sorry to hear this is giving you trouble. My physical test device for iOS is an iPhone 11 pro, currently running iOS 14.0.1. I just retested, and my app is still performing as expected. It is not, however, super fast... First the iPhone brings me to the general settings page, and then after a second or 3 it will dive into the settings for my particular app. I wonder if we are running different versions of iOS, and whether that could be the source of the problem? Have you tried a simulator running the same version of iOS as is on your iPhone 7? – jbachelor Nov 17 '20 at 23:52
1

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

AppInfo.ShowSettingsUI();

This should open settings for iOS and Android using Xamarin Essentials

jharrell
  • 46
  • 3