我如何请求用户的权限,自动启动和电池节省(没有限制)在Xamarin.Forms android?

d5vmydt9  于 2022-12-07  发布在  Android
关注(0)|答案(1)|浏览(141)

我正在为自己创建一个应用程序,我需要我的服务在设备重新启动时工作。我在这里做了我的代码。

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
    }
}

我还请求了使用文件夹和麦克风的权限。

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;
            }
        }

结果代码:

但是我遇到了一个问题,为了使我的服务在某些设备上正常工作,需要两个复选框。
现在我不明白如何询问用户这些权限,这样他就不用自己去设置了,也许应用程序可以自己打开这个页面,基本上小米手机都会出现这个问题,目前我在写android的应用程序,但是xamarin允许你写iOS的代码,所以以后我也会在那里添加这样的功能。

eblbsuwk

eblbsuwk1#

这是这个问题的答案

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 start Power Manager of all android manufactures to enable background and push notification?
只是我就是不明白怎么弄清楚旗的状况

相关问题