Search code examples
androidxamarinandroid-intentxamarin.androidtoast

“Rebooting receiver” not working android [Xamarin.Android]


I am trying to implement a broadcast receiver that gets the broadcast when the device has been rebooted, but is not working (it is supposed to send me a toast when the device has rebooted) with the following code:

Broadcast receiver:

    [BroadcastReceiver]
    public class RebootReceiver : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {
            if (Intent.ActionBootCompleted.Equals(intent.Action))
            {
                Toast.MakeText(
                    context,
                    "Your app has been rebooted!",
                    ToastLength.Long).Show();
            }
        }
    }

Manifest.xml

<receiver android:name=".RebootReceiver">
        <intent-filter>
          <action android:name="android.intent.action.BOOT_COMPLETED"></action>
        </intent-filter>
      </receiver>

And permission inside the manifest

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

Hope help, thanks


Solution

  • I have solved the problem, the answer of @FreakyAli has also helped in fact of getting the solution

    Create Service:

    [Service(Name = "com.companyname.app.RebootService")]
        public class RebootService : Service
        {
            public override void OnCreate()
            {
                base.OnCreate();
            }
    
            public override IBinder OnBind(Intent intent)
            {
                return null;
            }
            public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
            {
                Toast.MakeText(this, "Service STARTED!", ToastLength.Long).Show();
                return StartCommandResult.Sticky;
            }
    
            public override void OnDestroy()
            {
                base.OnDestroy();
    
                Toast.MakeText(this, "Service STOPED", ToastLength.Long).Show();
            }
        }
    }
    

    Create BroadcastReciver:

     [BroadcastReceiver(Enabled =true, Name ="com.companyname.Sortex.RebootReceiver")]
            [IntentFilter(new[] { Intent.ActionBootCompleted })]
            public class RebootReceiver : BroadcastReceiver
            {
                public override void OnReceive(Context context, Intent intent)
                {
                }
            }
    

    register Service and BroadcastReceiver on AndroidManifest.xml

    <service android:name="com.companyname.app.RebootService"/>
    <receiver android:name="com.companyname.app.RebootReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
    </receiver>
    

    Call the service on the OnReceive method of the Broadcast receiver:

    Intent serviceIntent = new Intent(context, typeof(RebootService));
    context.StartService(serviceIntent);