My BroadcastReceiver is not working when device is started

oflu1071oflu1071 TRMember
edited November 2016 in Cross Platform with Xamarin

I want to automatically start a service when the device is restarted. After that, I want the operations in the service to be executed. I wrote a code, but it fails when the device is rebooted. I have 2 classes. These are 'AdroidService' and 'DetectBootUp'. I think there is an error in 'AndroidManifest.xml'.

DetectBootUp.cs

namespace NotifyExp2.Droid.ReminderService { [BroadcastReceiver] [IntentFilter(new[] { Intent.ActionBootCompleted })] class DetectBootUp : BroadcastReceiver { public override void OnReceive(Context context, Intent intent) { Intent bootUp = new Intent(context.ApplicationContext, typeof(AndroidService)); bootUp.AddFlags(ActivityFlags.ExcludeStoppedPackages); bootUp.AddFlags(ActivityFlags.IncludeStoppedPackages); context.StartService(bootUp); } } }

AndroidService.cs

namespace NotifyExp2.Droid.ReminderService { [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 - " + DateTime.Now.Hour + ":" + DateTime.Now.Minute.ToString() + ":" + DateTime.Now.Second.ToString(), 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"
          android:installLocation="internalOnly">
    <uses-sdk android:minSdkVersion="15" />

  <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application android:label="NotificationExample"
               android:enabled="true">
    <receiver
      android:name=".DetectBootUp"
      android:enabled="true"
      android:exported="false"
      android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
      <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
        <category android:name="android.intent.category.HOME"/>
      </intent-filter>
    </receiver>

    <service android:name=".AndroidService"
             android:enabled="true"
             android:exported="false">
    </service>
  </application>

</manifest>`

Answers

  • prashantvcprashantvc USXamarin Team Xamurai

    Did you look into ADB logs for hints? Can you post the logs please?

Sign In or Register to comment.