Auto-Launching MAUI App on Startup

Auto-launching MAUI app on startup

  1. Android
    Use a BroadcastReceiver to receive the system boot completion message. There are two ways to implement broadcast reception.
    For Android 10 (test for other versions), you need to enable the overlay permission: <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />, located in Settings > Apps & Notifications > Apps > Advanced > Display over other apps > Allow.
    First Method: Using Attributes

    1. Remove startup-related permissions from the manifest file.

    2. Create a class BootBroadcastReceiver in the Platforms/Android folder, with BroadcastReceiver as the base class.

    3. Add the following attribute code above the namespace in the BootBroadcastReceiver.cs file.

      1
      2
      3
      4
      
      using Android;
      
      [assembly: UsesPermission(Manifest.Permission.SystemAlertWindow)]
      [assembly: UsesPermission(Manifest.Permission.ReceiveBootCompleted)]
      
    4. Add the following attribute code above the class.

      1
      2
      3
      
      [BroadcastReceiver(Enabled = true)]
      [IntentFilter(new[] { Intent.ActionBootCompleted })]    
      public class BootBroadcastReceiver : BroadcastReceiver
      
    5. Implement the abstract method OnReceive of the base class.

       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      
      public class BootBroadcastReceiver : BroadcastReceiver
      {
          public override void OnReceive(Context? context, Intent? intent)
          {
              Toast.MakeText(context, "Starting...", ToastLength.Long)?.Show();
              var new_intent = new Intent(context, typeof(MainActivity));
              new_intent.AddFlags(ActivityFlags.ResetTaskIfNeeded);
              new_intent.AddFlags(ActivityFlags.ReorderToFront);
              new_intent.AddFlags(ActivityFlags.NewTask);
              context.StartActivity(new_intent);
          }
      }
      

    Second Method: Modifying the Manifest File

    1. Add the following code to the AndroidManifest.xml file.

      1
      2
      
      <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
      <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
      
    2. Add the following code to the application section of the AndroidManifest.xml file.

      1
      2
      3
      4
      5
      
      <receiver android:name="com.yourpackagename.app.BootBroadcastReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
      </receiver>
      
    3. Create a class BootBroadcastReceiver in the Platforms/Android folder, with BroadcastReceiver as the base class.

    4. Add the following attribute code above the class.

      1
      2
      3
      
      [BroadcastReceiver(Name = "com.yourpackagename.app.BootBroadcastReceiver", Enabled = true)]
      [IntentFilter(new[] { Intent.ActionBootCompleted })]    
      public class BootBroadcastReceiver : BroadcastReceiver
      
    5. Implement the abstract method OnReceive of the base class.

       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      
      public class BootBroadcastReceiver : BroadcastReceiver
      {
          public override void OnReceive(Context? context, Intent? intent)
          {
              Toast.MakeText(context, "Starting...", ToastLength.Long)?.Show();
              var new_intent = new Intent(context, typeof(MainActivity));
              new_intent.AddFlags(ActivityFlags.ResetTaskIfNeeded);
              new_intent.AddFlags(ActivityFlags.ReorderToFront);
              new_intent.AddFlags(ActivityFlags.NewTask);
              context.StartActivity(new_intent);
          }
      }
      
  2. Windows
    Use the command start program_name: to launch the program, place the command in a .bat file, and add it to the startup folder.

Licensed under CC BY-NC-SA 4.0
Built with Hugo
Theme Stack designed by Jimmy