-3

I am trying to make my app start when my device reboots, so I created a class called BootReceiver extending from BroadcastReceiver with an intent filter registered with the Android.intent.action.BOOT_COMPLETED constant.

I created an instance of the BootReceiver class in my Oncreate()... and registered it with the filter specified above. I also declared the permission for receiving boot completed intents on my manifest file and requested for it programmatically. Even after doing all that, my app does not start when restart my device.

BootReceiver class

[BroadcastReceiver(Label ="BootReceiver", Enabled = true, Exported = true)]
[IntentFilter(new[] { Android.Content.Intent.ActionBootCompleted })]
class BootReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        // Launch our activity
        if (intent.Action == "android.intent.action.BOOT_COMPLETED")
        {
            /*
               Intent new_intent = new Intent(context, typeof(MainActivity));
                  new_intent.SetFlags(ActivityFlags.NewTask);
                  context.StartActivity(new_intent);
            */

            Toast.MakeText(context, "trying to start Niskize app", ToastLength.Long).Show();
        }
    }
}

MainActivity class

[Activity(Label = "@string/app_name", Theme = "@style/AppThemeWhite", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
     BootReceiver bootReceiver;

     protected override void OnCreate(Bundle savedInstanceState)
     {
         // Request for boot completed permissions
         boot_permissions();
         bootReceiver = new BootReceiver();
         RegisterReceiver(bootReceiver, new IntentFilter("android.intent.action.BOOT_COMPLETED"));
     }
}

How can I implement this correctly with Android Xamarin?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Son of Man
  • 1,213
  • 2
  • 7
  • 27

1 Answers1

1

I had tried your code on different versions of Android. If the device is with Android 9.0 or lower, the activity will start successfully. But on the Android 10.0 or higher, the system will intercept the starting of the activity background.

More information: https://developer.android.com/guide/components/activities/background-starts

And if you start the app at once when the device is on boot, you can seem the activity will flash once, this is because the activity is foreground and will not be intercept by the system.

I also tried to register the BootReceiver by the AndroidManifest.xml and on the Android 12 you need to add the android:exported="true" to the receiver.

AndroidManifest.xml:

<receiver android:enabled="true" android:name=".BootReceiver">
    <intent-filter>
        <action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
    </intent-filter>
</receiver>

BootReceiver.cs

[BroadcastReceiver(Label = "BootReceiver", DirectBootAware = true,Enabled = true, Exported = true)]
[IntentFilter(new[] { Android.Content.Intent.ActionBootCompleted })]
class BootReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        //launch our activity
        if (intent.Action == "android.intent.action.BOOT_COMPLETED")
        { 
            Toast.MakeText(context, "trying to start Niskize app", ToastLength.Long).Show();
            Intent new_intent = new Intent(context, typeof(MainActivity));
            new_intent.SetFlags(ActivityFlags.NewTask);
            context.StartActivity(new_intent);
           
        }
    }
}

I put the Toast on the first line to ensure the bootreceiver works correctly and just the startactivity failed.

In addition, on the Android 10 and higher, it seems that if the developer wants the app auto start when the device is on boot, need to add the app to the white list.

Liyun Zhang - MSFT
  • 8,271
  • 1
  • 2
  • 14
  • what is "the white list" and how do you add an app to it please? – Netricity Mar 07 '23 at 11:09
  • With whitelist, I guess that Liyun means go to seetings-->applications-->permissions-->auto start. Here you can set which application you want it starts on OS startup. It is configuration in the settings. – Álvaro García May 05 '23 at 09:44