[英]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
这是一张图片
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.