私のAndroidManifest
とBroadcastreceiver
とサービスクラスを以下に示します。デバイスの起動時に私の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を知っていません:) – oflu1071