adb – how to install user application on android that can run forever on device

I am newbie in android and i want to make an app that can run on device as soon as device boots up.
i am using android 10 and the device i am using is headless device so whatever i’ll do will be from command line only.
i found some code on internet that can run my app as soon as device is turned on.when i install the app from android studio and reboots the device several times it works as expected.
when i install the same app using “adb install -g -t file.apk”,it run for 10-15 seconds as soon as device is turned on and then it stop working.(i am using ps -e to see running processes)
i do not understand this behaviour. Can someone help me to understand the what is wrong and how i can run my app forever/automatically start the app on device re(boot).

Thanks in advance

code i am using is as follow-
public class StartMyActivityAtBootReceiver extends BroadcastReceiver {

public void onReceive(Context context, Intent intent) {
    if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
        Intent activityIntent = new Intent(context, MainActivity.class);


in .xml file–