-2

如何在 Android 和 ios 的 Xamarin.Forms 中打开/关闭飞行模式?

4

1 回答 1

0

正如@Patrick Hofman 和@DavidG 所说。您无法通过项目中的代码打开/关闭飞行模式。但是,您可以使用依赖服务在Forms中打开系统设置页面,让用户手动打开/关闭飞行模式。

在表单中,添加一个新界面

public interface ISettingsService
{
  void OpenSettings();
}

在 iOS 中

using xxx.iOS;
using Foundation;
using UIKit;
using Xamarin.Forms;

[assembly: Dependency(typeof(OpenSettingsImplement))]
namespace xxx.iOS
{
    public class OpenSettingsImplement : ISettingsService
    {
        public void OpenSettings()
        {
            var url = new NSUrl($"App-Prefs:");
            UIApplication.SharedApplication.OpenUrl(url);
        }
    }
}

笔记:

App-Prefs:是 iOS 中的私有 api。所以如果你想上传到应用商店,它可能会被应用商店拒绝。

在安卓中

using Android.Content;

using xxx;
using xxx.Droid;
using Xamarin.Forms;

[assembly: Dependency(typeof(OpenSettingsImplement))]
namespace xxx.Droid
{
    public class OpenSettingsImplement : ISettingsService
    {
        public void OpenSettings()
        {
            Intent intentOpenSettings = new Intent();
            intentOpenSettings.SetAction(Android.Provider.Settings.ActionAirplaneModeSettings);
            Android.App.Application.Context.StartActivity(intentOpenSettings);
        }
    }
}
于 2019-07-10T05:08:51.850 回答