-
Android
Use aBroadcastReceiver
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-
Remove startup-related permissions from the
manifest
file. -
Create a class
BootBroadcastReceiver
in thePlatforms/Android
folder, withBroadcastReceiver
as the base class. -
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)]
-
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
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
-
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"/>
-
Add the following code to the
application
section of theAndroidManifest.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>
-
Create a class
BootBroadcastReceiver
in thePlatforms/Android
folder, withBroadcastReceiver
as 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
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); } }
-
-
Windows
Use the commandstart program_name:
to launch the program, place the command in a.bat
file, and add it to the startup folder.