-
Android
Use aBroadcastReceiverto 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-
Remove startup-related permissions from the
manifestfile. -
Create a class
BootBroadcastReceiverin thePlatforms/Androidfolder, withBroadcastReceiveras the base class. -
Add the following attribute code above the namespace in the
BootBroadcastReceiver.csfile.1 2 3 4using Android; [assembly: UsesPermission(Manifest.Permission.SystemAlertWindow)] [assembly: UsesPermission(Manifest.Permission.ReceiveBootCompleted)] -
Add the following attribute code above the class.
1 2 3[BroadcastReceiver(Enabled = true)] [IntentFilter(new[] { Intent.ActionBootCompleted })] public class BootBroadcastReceiver : BroadcastReceiver -
Implement the abstract method
OnReceiveof the base class.1 2 3 4 5 6 7 8 9 10 11 12public 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
-
Add the following code to the
AndroidManifest.xmlfile.1 2<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> -
Add the following code to the
applicationsection of theAndroidManifest.xmlfile.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> -
Create a class
BootBroadcastReceiverin thePlatforms/Androidfolder, withBroadcastReceiveras the base class. -
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 -
Implement the abstract method
OnReceiveof the base class.1 2 3 4 5 6 7 8 9 10 11 12public 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); } }
-
-
Windows
Use the commandstart program_name:to launch the program, place the command in a.batfile, and add it to the startup folder.