Мои AndroidManifest
, Broadcastreceiver
и классы обслуживания показаны ниже.My BroadcastReceiver не работает, когда устройство запущено
DetectBootUp.cs:
[BroadcastReceiver]
[IntentFilter(new[] { Intent.ActionBootCompleted })]
public class DetectBootUp : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
Intent bootUp = new Intent(context, typeof(AndroidService));
context.StartService(bootUp);
}
}
AndroidService.cs
[Service]
public class AndroidService : Service
{
public override void OnCreate()
{
Toast.MakeText(this, "Service Created", ToastLength.Long).Show();
Log.Debug("BroadCastReceiverBoot", "OnCreate");
}
public override IBinder OnBind(Intent intent)
{
return null;
}
public override void OnDestroy()
{
Toast.MakeText(this, "Service Destroyed", ToastLength.Long).Show();
Log.Debug("BroadCastReceiverBoot", "onDestroy");
ApplicationContext.StartService(new Intent(ApplicationContext, typeof(AndroidService)));
}
[return: GeneratedEnum]
public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
{
Toast.MakeText(this, "Service Started", ToastLength.Long).Show();
Log.Debug("BroadCastReceiverBoot", "OnStart");
return StartCommandResult.Sticky;
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:label="NotificationExample">
<receiver
android:name=".DetectBootUp"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
<service android:name=".AndroidService"
android:enabled="true"
android:exported="false">
</service>
</application>
</manifest>
MainActivity.cs
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
StartService(new Android.Content.Intent(Application.Context, typeof(AndroidService)));
}
}
Когда устройство перезагружается, я хочу, чтобы процессы внутри класса служб выполнялись. Но это дает ошибку. Почему? Когда я запускаю приложение, он работает нормально, но я хочу, чтобы он выполнялся автоматически при перезапуске устройства. Вероятно, на приемнике есть ошибка, но я не могу найти, где.
Какая ошибка? У вас есть стек? – Demitrian
Не имеет stacktrace. Я не знаю stacktrace :) – oflu1071