如何使用 Xamarin.Essentials Launcher 将 Android 设置应用启动到您的应用设置? [英] How to launch Android Settings app to your app's settings with Xamarin.Essentials Launcher?

查看:40
本文介绍了如何使用 Xamarin.Essentials Launcher 将 Android 设置应用启动到您的应用设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Xamarin.Forms 和 Xamarin.Essentials 开发适用于 Android 和 iOS 的应用程序.我希望能够在我的应用中提供一个按钮,将用户带到设备的设置"应用,然后进入我正在开发的这个应用的特定设置页面.

以前,这是使用依赖项服务调用特定于平台的代码来完成的.我相信使用 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!

解决方案

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://docs.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://docs.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:

这篇关于如何使用 Xamarin.Essentials Launcher 将 Android 设置应用启动到您的应用设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆