简体   繁体   English

如何在 Xamarin.Forms android 中向用户请求自动启动和省电模式(无限制)的权限?

[英]How can I request permission from the user for autostart and for Battery saver(No restrictions) in Xamarin.Forms android?

I am creating an application for myself, and I need my service to work when the device is rebooted.我正在为自己创建一个应用程序,并且我需要我的服务在设备重新启动时工作。 I did it here is my code.我在这里做的是我的代码。

namespace Corporate_messenger.Droid.Broadcast
{
    [BroadcastReceiver(Name = "com.companyname.corporate_messenger.BootReceiver", Enabled = true, Exported = true)]
    [IntentFilter(new[] { Android.Content.Intent.ActionBootCompleted })]
    public class BootReceiver : BroadcastReceiver
    {
        private void Start1(Context context)
        {
            Intent mycallIntent = new Intent(context, typeof(MainActivity));

            mycallIntent.AddFlags(ActivityFlags.NewTask);

           


            Android.App.Application.Context.StartActivity(mycallIntent);
        }
        public override void OnReceive(Context context, Intent intent)
        {
            try
            {
               

              
                var intentService = new Intent(context, typeof(NotoficationService));
                if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
                {
                    SpecialData.RestartResponse = true;
                    context.StartForegroundService(intentService);

                }
                else
                {
                    context.StartService(intentService);
                    // Flag_On_Off_Service = true;
                }
            }
            catch(Exception ex)
            {
                Log.Error("MyLog", ex.Message);

            }


        } // OnReceive
    }
}

I also requested permissions to work with folders and microphone.我还请求了使用文件夹和麦克风的权限。 My cod - Permission我的鳕鱼 - 许可

 public async Task Permission()
        {
            
            var PermissionsStrorage_Write = await Permissions.CheckStatusAsync<Permissions.StorageWrite>();

            var PermissionsInternet = await Permissions.CheckStatusAsync<Permissions.NetworkState>();

            var PermissionsStrorage_Read = await Permissions.CheckStatusAsync<Permissions.StorageRead>();

            var PermissionsRecord = await Permissions.CheckStatusAsync<Permissions.Microphone>();
            if (PermissionsInternet != PermissionStatus.Granted)
            {
                PermissionsInternet = await Permissions.RequestAsync<Permissions.NetworkState>();

            }
            
            if (PermissionsRecord != PermissionStatus.Granted)
            {
                PermissionsRecord = await Permissions.RequestAsync<Permissions.Microphone>();

            }
          
            if (PermissionsStrorage_Write != PermissionStatus.Granted && PermissionsStrorage_Read != PermissionStatus.Granted)
            {
                PermissionsStrorage_Write = await Permissions.RequestAsync<Permissions.StorageWrite>();
                PermissionsStrorage_Read = await Permissions.RequestAsync<Permissions.StorageRead>();
            }
            if (PermissionsStrorage_Write != PermissionStatus.Granted && PermissionsStrorage_Read != PermissionStatus.Granted)
            {
                return;
            }
        }

Result code:结果代码: 结果代码 But I ran into a problem, which is that for my service to work correctly on some devices, two checkboxes are required.但是我遇到了一个问题,即为了让我的服务在某些设备上正常工作,需要两个复选框。 Here's a picture这是一张图片2 个复选框

Now I don't understand how to ask the user about these permissions so that he doesn't have to go into the settings himself.现在我不明白如何向用户询问这些权限,以便他不必自己进入设置 go 。 Perhaps the application could open this page on its own.Basically, this problem occurs on xiaomi phone.也许应用程序可以自己打开这个页面。基本上,这个问题出现在小米手机上。 At the moment I am writing an application for android.目前我正在为 android 编写应用程序。 But xamarin allows you to write code for iOS, so in the future I will also add such functions there.但是 xamarin 允许你为 iOS 编写代码,所以以后我也会在那里添加这样的功能。

here is the answer to this question这是这个问题的答案

 private void SetSetting()
        {
           
            // Manufacturer (Samsung)
            var manufacturer = DeviceInfo.Manufacturer.ToLower();
            switch (manufacturer)
            {
                case "xiaomi":
                    SetPermission("com.miui.powerkeeper", "com.miui.powerkeeper.ui.HiddenAppsConfigActivity");
                    break;
                case "huawei":
                    SetPermission("com.huawei.systemmanager", "com.huawei.systemmanager.appcontrol.activity.StartupAppControlActivity");
                    break;
                case "samsung":
                    if(Battery.EnergySaverStatus == EnergySaverStatus.On)
                        SetPermission("com.samsung.android.lool", "com.samsung.android.sm.battery.ui.BatteryActivity");
                    break;
            }
           
        }

        private void SetPermission(string param1,string param2)
        {
            try
            {
                Intent intent = new Intent();
                intent.SetComponent(new ComponentName(param1, param2));
                // intent.SetComponent(new ComponentName("com.miui.securitycenter", "com.miui.appmanager.ApplicationsDetailsActivity"));
                intent.PutExtra("package_name", PackageName);

                StartActivity(intent);

            }
            catch (Exception anfe)
            {
            }
        }

How to get into settings on other devices How to start Power Manager of all android manufactures to enable background and push notification?如何进入其他设备的设置如何启动所有 android 制造商的电源管理器以启用后台和推送通知?

Only I just don't understand how to find out the status of the flag只有我只是不明白如何找出标志的状态

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Xamarin.Forms 获得连续的地理定位? - How can I get a continous geolication from Xamarin.Forms? Android权限对话框未在Xamarin.Forms中显示 - Android permission dialog not showing in Xamarin.Forms 如何从 Android MainActivity.cs 文件中更改 Xamarin.forms 中页面 Label 的文本? - How can I change the text of a page's Label in Xamarin.forms from the Android MainActivity.cs file? 如何允许用户在Xamarin.Forms的ListView中平移地图? - How can I allow a user to pan a map within a ListView in Xamarin.Forms? 如何访问 Xamarin.forms 中的通讯录(适用于 android 和 iOS)? - How can I access the address book in Xamarin.forms (for android and iOS)? 如何更改原始选项卡页面栏的样式(android with xamarin.forms)? - How can I change style of the original tabbed page bar (android with xamarin.forms)? 如何查看我的 Xamarin.Forms 应用程序是否在 Visual Studio 中的 Android 6(或更高版本)上运行或/和可运行? - How can I see if my Xamarin.Forms app is working or/and runnable on Android 6 (or higher) in Visual Studio? 如何在Android的Xamarin.forms上正确使用活动指示器? - How can I use correctly the activity indicator on Xamarin.forms for Android? 在显示Xaml中设置的地图之前如何请求位置权限。 Xamarin表格 - How to Request Location permission before showing map set up in Xaml. Xamarin.Forms 如何在Xamarin.Forms中为ToolbarItem创建自定义渲染器? - How can I make a custom renderer for ToolbarItem in Xamarin.Forms?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM