Xamarin禁用广播接收器

时间:2017-06-28 09:55:08

标签: c# android xamarin xamarin.android

我希望禁用我附加到boot_completed操作的广播接收器。接收器按预期工作但是我希望在用户禁用我的更新程序时禁用此接收器,详细信息位于https://developer.android.com/training/scheduling/alarms.html的底部。

我尝试使用的代码是:

ComponentName receiver = new ComponentName(context, "com.app.bootcompleted");
PackageManager pm = context.PackageManager;
pm.SetComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DontKillApp);

但是PackageManager不包含: COMPONENT_ENABLED_STATE_ENABLED

Xamarin Android 中禁用广播接收器有不同的方法吗?

1 个答案:

答案 0 :(得分:1)

文档似乎已过时https://developer.xamarin.com/api/member/Android.Content.PM.PackageManager.SetComponentEnabledSetting/

我已设法使用以下代码启用/禁用boradcast接收器:

//Stop the boot_complete receiver from starting the alarm
ComponentName receiver = new ComponentName(context, "com.app.bootCompleteReceiver");
PackageManager pm = context.PackageManager;
pm.SetComponentEnabledSetting(receiver, ComponentEnabledState.Disabled, ComponentEnableOption.DontKillApp);

还要确保正确装饰广播接收器:

[BroadcastReceiver (Name = "com.app.bootCompleteReceiver", Enabled = false)]
public class BootCompleteReceiver : BroadcastReceiver { ...