如何使用 Xamarin.Essentials 启动器将 Android 设置应用程序启动到您的应用程序设置?
How to launch Android Settings app to your app's settings with Xamarin.Essentials Launcher?
我正在使用 Xamarin.Forms 和 Xamarin.Essentials 为 Android 和 iOS 开发应用程序。我希望能够在我的应用程序中提供一个按钮,将用户带到设备的 'Settings' 应用程序,然后进入我正在开发的这个应用程序的特定设置页面。
以前,这是使用依赖服务调用特定于平台的代码来完成的。我相信可以使用 Xamarin.Essentials Launcher (Microsoft documentation here) 更简单地完成此操作,并且无需任何特定于平台的代码。我已经想出如何在 iOS:
上执行此操作
bool didOpenUri = await Xamarin.Essentials.Launcher.TryOpenAsync("app-settings:com.yourCompany.yourApp");
但是,我无法在 Android 上找到如何执行此操作。如果有任何不清楚的地方,请告诉我。否则,我们将不胜感激...非常感谢!
据我所知,Android 设置没有 URL。启动器不支持它。
但是,设置 class 为“设置”应用程序提供操作字符串。
设置Class:https://docs.microsoft.com/en-us/dotnet/api/android.provider.settings?view=xamarin-android-sdk-9
您可以在 Android 中执行此操作:
StartActivity(new Intent(Settings.ActionSettings));
并使用依赖服务在 Xamarin.Forms 中做到这一点。
更新:
在Xamarin.Forms中:
创建接口:
public interface IStartSetttings
{
void StartSettings();
}
实施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);
}
}
}
在Xamarin.Forms中的用法:
DependencyService.Get<IStartSetttings>().StartSettings();
截图:
@WendyZang 非常友好地提供了原始问题的答案,很遗憾,您无法使用 Xamarin.Essentials Launcher.
相反,您必须使用具有平台特定实现的依赖服务。为了帮助 运行 遇到同样问题的其他人,我想添加一个在 iOS 和 Android 上设置依赖服务的完整解决方案,它不仅导航到OS 的设置应用程序,但专门针对 您的 应用程序的设置:
在您的跨平台项目中,界面可能如下所示。您需要传入应用程序的 bundle-id(例如,com.myCompany.myApp
):
namespace MyCoolMobileApp.Services.DependencyServices
{
public interface ISettingsAppLauncher
{
void LaunchSettingsApp(string appBundleId);
}
}
在 Android 中,您的实现可能如下所示(请注意,我使用的是 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);
}
}
}
最后但同样重要的是,iOS 实施将是:
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);
}
}
}
https://github.com/xamarin/Essentials/pull/328
AppInfo.ShowSettingsUI();
这应该使用 Xamarin Essentials 打开 iOS 和 Android 的设置
我正在使用 Xamarin.Forms 和 Xamarin.Essentials 为 Android 和 iOS 开发应用程序。我希望能够在我的应用程序中提供一个按钮,将用户带到设备的 'Settings' 应用程序,然后进入我正在开发的这个应用程序的特定设置页面。
以前,这是使用依赖服务调用特定于平台的代码来完成的。我相信可以使用 Xamarin.Essentials Launcher (Microsoft documentation here) 更简单地完成此操作,并且无需任何特定于平台的代码。我已经想出如何在 iOS:
上执行此操作bool didOpenUri = await Xamarin.Essentials.Launcher.TryOpenAsync("app-settings:com.yourCompany.yourApp");
但是,我无法在 Android 上找到如何执行此操作。如果有任何不清楚的地方,请告诉我。否则,我们将不胜感激...非常感谢!
据我所知,Android 设置没有 URL。启动器不支持它。
但是,设置 class 为“设置”应用程序提供操作字符串。
设置Class:https://docs.microsoft.com/en-us/dotnet/api/android.provider.settings?view=xamarin-android-sdk-9
您可以在 Android 中执行此操作:
StartActivity(new Intent(Settings.ActionSettings));
并使用依赖服务在 Xamarin.Forms 中做到这一点。
更新:
在Xamarin.Forms中:
创建接口:
public interface IStartSetttings
{
void StartSettings();
}
实施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);
}
}
}
在Xamarin.Forms中的用法:
DependencyService.Get<IStartSetttings>().StartSettings();
截图:
@WendyZang 非常友好地提供了原始问题的答案,很遗憾,您无法使用 Xamarin.Essentials Launcher.
相反,您必须使用具有平台特定实现的依赖服务。为了帮助 运行 遇到同样问题的其他人,我想添加一个在 iOS 和 Android 上设置依赖服务的完整解决方案,它不仅导航到OS 的设置应用程序,但专门针对 您的 应用程序的设置:
在您的跨平台项目中,界面可能如下所示。您需要传入应用程序的 bundle-id(例如,com.myCompany.myApp
):
namespace MyCoolMobileApp.Services.DependencyServices
{
public interface ISettingsAppLauncher
{
void LaunchSettingsApp(string appBundleId);
}
}
在 Android 中,您的实现可能如下所示(请注意,我使用的是 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);
}
}
}
最后但同样重要的是,iOS 实施将是:
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);
}
}
}
https://github.com/xamarin/Essentials/pull/328
AppInfo.ShowSettingsUI();
这应该使用 Xamarin Essentials 打开 iOS 和 Android 的设置