Reciever не ловит BOOT_COMPLETED после перезагрузки устройства
Смотрел много вопросов на тему создания Service после перезагрузки устройства, в одном ответе прочитал, что ActionBootCompleted ловится только при открытии приложении пользователем (у меня сейчас также). На других сайтах у авторов прекрасно создаются сервисы сразу после перезагрузки устройства и без открытия приложения. В документации сказано, что в Android 8.0 ввели ограничения на создание Service.
Как мне получить BootCompleted в Reciever после перезагрузки устройства, не открывая приложение (Android 9.0)?
[IntentFilter(new string[] { Intent.ActionBootCompleted, Intent.ActionLockedBootCompleted, "android.intent.action.QUICKBOOT_POWERON", "com.htc.intent.action.QUICKBOOT_POWERON" },
Priority = (int)IntentFilterPriority.HighPriority)]
[BroadcastReceiver(Enabled = true, Exported = true, Permission = "android.permission.RECEIVE_BOOT_COMPLETED", DirectBootAware = true)]
public class ScheduledAlarmHandler : BroadcastReceiver
{
private void startServiceDirectly(Context context)
{
string message = "BootDeviceReceiver onReceive start service directly.";
Toast.MakeText(Application.Context, message, ToastLength.Long).Show();
Intent startServiceIntent = new Intent(context, typeof(LocalNotificationService));
context.StartService(startServiceIntent);
}
public override void OnReceive(Context context, Intent intent)
{
if (intent.Action.Equals(Intent.ActionBootCompleted))
{
Toast.MakeText(context.ApplicationContext, "ActionBootCompleted", ToastLength.Long).Show();
startServiceDirectly(context);
}
else if (intent.Action.Equals("android.intent.action.QUICKBOOT_POWERON"))
{
Toast.MakeText(context.ApplicationContext, "QUICKBOOT_POWERON", ToastLength.Long).Show();
}
else
{
Toast.MakeText(context.ApplicationContext, intent.Action, ToastLength.Long).Show();
}
}
[IntentFilter(new string[] { Intent.ActionBootCompleted, Intent.ActionLockedBootCompleted, "android.intent.action.QUICKBOOT_POWERON", "com.htc.intent.action.QUICKBOOT_POWERON" },
Priority = (int)IntentFilterPriority.HighPriority)]
[Service(Enabled =true, Exported = true, Permission = "android.permission.RECEIVE_BOOT_COMPLETED", DirectBootAware = true)]
public class LocalNotificationService: Service
{
public override IBinder OnBind(Intent intent)
{
throw new NotImplementedException("Not yet implemented");
}
public override void OnCreate()
{
base.OnCreate();
}
[return: GeneratedEnum]
public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
{
String message = "RunAfterBootService onStartCommand() method.";
Toast.MakeText(Application.Context, message, ToastLength.Long).Show();
return base.OnStartCommand(intent, flags, startId);
}
public override void OnDestroy()
{
base.OnDestroy();
}
public void LocalNotification(int id, DateTime notifyTime)
{
long repeateForMinute = 15000; // In milliseconds
long totalMilliSeconds = (long)(notifyTime.ToUniversalTime() - _jan1st1970).TotalMilliseconds;
if (totalMilliSeconds < Java.Lang.JavaSystem.CurrentTimeMillis())
{
totalMilliSeconds = totalMilliSeconds + repeateForMinute;
}
var intent = CreateIntent(id);
var pendingIntent = PendingIntent.GetBroadcast(Application.Context, 0, intent, PendingIntentFlags.Immutable);
var alarmManager = GetAlarmManager();
alarmManager.SetInexactRepeating(AlarmType.RtcWakeup, totalMilliSeconds, repeateForMinute, pendingIntent);
}
public static void Cancel(int id)
{
var intent = CreateIntent(id);
var pendingIntent = PendingIntent.GetBroadcast(Application.Context, 0, intent, PendingIntentFlags.Immutable);
var alarmManager = GetAlarmManager();
alarmManager.Cancel(pendingIntent);
var notificationManager = NotificationManagerCompat.From(Application.Context);
notificationManager.Cancel(id);
}
public static Intent GetLauncherActivity()
{
var packageName = Application.Context.PackageName;
return Application.Context.PackageManager.GetLaunchIntentForPackage(packageName);
}
public static Intent CreateIntent(int id)
{
return new Intent(Application.Context, typeof(ScheduledAlarmHandler)).SetAction("LocalNotifierIntent" + id);
}
private static AlarmManager GetAlarmManager()
{
var alarmManager = Application.Context.GetSystemService(Context.AlarmService) as AlarmManager;
return alarmManager;
}
}