How can I start android application on device boot?

Check this. In this way what you want to achieve should work. If you can not, logs should be followed to find out the problem.

  public class AutoStart  extends BroadcastReceiver
        {
            @Override
            public void onReceive(Context context, Intent arg1)
            {

                try {
                    Intent intent = new Intent(context, WorkerService.class);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(intent);
        } else {
            context.startService(intent);

                    }

                }catch(Exception ex) {

                    Toast.makeText(context, ex.getMessage(), Toast.LENGTH_LONG).show();
                }
}
}

In Service

public class WorkerService extends Service {
    public static final String CHANNEL_ID = "ForegroundServiceChannel";
    public static final String NEW_CHANNEL_ID = "AndroidForegroundServiceChannel";

    Notification notification;

    @Override
    public void onCreate() {
        super.onCreate();

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {createNotificationChannel();  //for Android Oreo above notification channel mandatory }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        try {  

            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
            {// if Android 10 create a pending intent and a full screen notification

    Intent fullScreenIntent = new Intent(this, "Your Activity".class);
    PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(this, 2022,
            fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT); // For the activity opening when notification cliced

    notification= new NotificationCompat.Builder(this, NEW_CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_notification)
            .setContentTitle("Notification title")
            .setContentText("Notification Text")
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setCategory(NotificationCompat.CATEGORY_REMINDER)
            .setFullScreenIntent(fullScreenPendingIntent, true)
            .build();

    startForeground(2, notification);
}
            else
            {
//if below Android 10 created a notification for foreground service because it is mandatory
            Intent notificationIntent = new Intent(this, Your Activity.class);
            PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0022,
                    notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                    .setContentText("Foreground Service")
                    .setSmallIcon(R.drawable.ic_notification)
                    .setSound(null)
                    .setContentIntent(pendingNotificationIntent)
                    .build();

             //for below Android 10 started activity
                    Intent i = new Intent(getApplicationContext(), Your Activity.class);
                    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);                     
                    getApplicationContext().startActivity(i);
                }


            startForeground(1, notification);
        }
        }
        catch (Exception e)
        {
            Toast.makeText(getApplicationContext(), "Foreground Service fault", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }

        return START_NOT_STICKY;
    }
   private void createNotificationChannel() {

      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            NotificationChannel serviceChannel = new NotificationChannel(
                    NEW_CHANNEL_ID,
                    "Android Foreground Service Channel",
                    NotificationManager.IMPORTANCE_HIGH
            );

            serviceChannel.setSound(null,null);
            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(serviceChannel);

        }

      else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel serviceChannel = new NotificationChannel(
                    CHANNEL_ID,
                    "Foreground Service Channel",
                    NotificationManager.IMPORTANCE_DEFAULT
            );

            serviceChannel.setSound(null,null);
            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(serviceChannel);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

After I added the boot permissions in manifest file (see code below), BroadcastReceiver started to get boot completed event and the service is starting successfully. For the solution to work (as suggested by @Eren Tüfekçi) I had to enable auto start permission in phone settings for my application. If anyone has a solution, how to enable it programmatically, please let us know. Thank you.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="kor.location.tracker">


    <uses-permission android:name="android.permission.INTERNET" ></uses-permission>
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <receiver android:name="kor.location.tracker.AutoStart"
            android:enabled="true"
            android:exported="true">
            <intent-filter android:directBootAware="true">
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
                <action android:name="android.intent.action.REBOOT"/>
            </intent-filter>
        </receiver>


        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <service android:enabled="true"
            android:name="kor.location.tracker.WorkerService"
            android:exported="true"
            android:permission="android.permission.BIND_JOB_SERVICE"
            />

    </application>

</manifest>

I can still start the app on Boot. (Targeting API 29). By using broadcast receiver with intent ACTION_BOOT_COMPLETED.

The problem I faced after updating my android to version 9 on Honor brand of phone was introduction of advance app management for probably battery preservation which prevented my app from receiving the broadcast in the first place.

Go to Settings > Battery > App Launch > Go to your app and uncheck "Manage Automatically" > And make sure "Auto-launch", "Secondary launch", "Run in background" is checked and select "OK"

Reboot your phone and check if the app starts on boot or not. Hope this helps someone else.


1- For Problem Starting Activity From Background

in API29 Android restricted, starting an activity from the background. The foreground service is also considered a background process. Your activity can be affected by this restriction if you test it in Android 10.

Android Q Restrictions: https://developer.android.com/guide/components/activities/background-starts

Possible solution: https://stackoverflow.com/a/59421118/11982611

2- Some brands limits applications to start in the boot to boost start up time of the device. So applications need exclusive permission to start in the boot.

Possible Solution (Programmatically) : https://stackoverflow.com/a/49167712/11982611

For Xiaomi, enabling Autostart from settings https://dontkillmyapp.com/xiaomi