'Flutter - Start a Service immediately after the app booted on Android

I want to start my app at boot i have added all the permission in mainifest file and have created a service file there is no error app runs fine , but it does not start at the boot .

Permission added :

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

My mainifest file

<receiver
          android:enabled="true"
          android:exported="true"
          android:name="com.example.vediostreaming.StartMyServiceAtBootReceiver"
          android:permission="android.permission.RECEIVE_BOOT_COMPLETED">

          <intent-filter>
              <action android:name="android.intent.action.BOOT_COMPLETED" />
              <action android:name="android.intent.action.QUICKBOOT_POWERON" />
              <category android:name="android.intent.category.DEFAULT" />
          </intent-filter>

      </receiver>```

and i have added a service file in the android/app/src/main/kotlin folder

package com.example.vediostreaming
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;


class StartMyServiceAtBootReceiver : BroadcastReceiver() {

    @Override
    override fun    onReceive(context: Context, intent: Intent) {
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            val serviceIntent = Intent(context, MainActivity::class.java)
            serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            context.startService(serviceIntent)
        }
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source