1

I'd like my Flutter/Kotlin application to be able to launch and display automatically when my Android 9 and higher device starts up.

I've tried the broadcastReceiver but it doesn't work, I receive the logs but it doesn't visually launch the application.

I add autorization to my Manifest

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

and declaration of my BroadcastReceiver

 <receiver android:name=".BootBroadcastReceiver" android:enabled="true" android:exported="false" android:permission="android.permission.RECEIVE_BOOT_COMPLETED"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver> 

I created two classes, a class BroadcastReceiver who get boot_complete

 import androidx.work.OneTimeWorkRequest import androidx.work.WorkManager import androidx.work.WorkRequest class BootBroadcastReceiver : BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) { if (intent != null) { if(intent.action == Intent.ACTION_BOOT_COMPLETED) { Log.d("BootBroadcastReceiver", "Boot completed. Scheduling app launch.") // Planifier un travail pour démarrer l'application après un délai val workRequest: WorkRequest = OneTimeWorkRequest.Builder(AppLaunchWorker::class.java).build() if (context != null) { WorkManager.getInstance(context).enqueue(workRequest) } } } } } 

and a class AppLaunchWorker who launch the app

 import android.content.Context import android.content.Intent import androidx.work.Worker import androidx.work.WorkerParameters import io.flutter.embedding.android.FlutterActivity class AppLaunchWorker(context: Context, workerParams: WorkerParameters) : Worker(context, workerParams) { override fun doWork(): Result { // Lancer FlutterActivity après que le système ait démarré val launchIntent = FlutterActivity.createDefaultIntent(applicationContext) launchIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK // Nécessaire pour démarrer l'activité depuis un contexte non interactif applicationContext.startActivity(launchIntent) // Retourner un résultat de succès return Result.success() } } 

i can see the log but the app dont open visually

 Log.d("BootBroadcastReceiver", "Boot completed. Scheduling app launch.") 
6
  • Do you have Handled Android 9+ Restrictions ? Battery optimizer and/or auto-start in the settings of the phone ? Commented Nov 26, 2024 at 7:58
  • 1
    android.permission.ACTION_MANAGE_OVERLAY_PERMISSION android.permission.SYSTEM_ALERT_WINDOW did you granted the permission? look like your problem is similar to stackoverflow.com/questions/72972225/… Commented Nov 26, 2024 at 9:28
  • 1
    Thank you both for your help im checking that Commented Nov 26, 2024 at 11:24
  • 1
    I think I understood that since Android 9 it was impossible to have the application open automatically in the foreground and that this required at least a notification to open the application, could someone confirm this? Commented Nov 26, 2024 at 12:36
  • I'm not sure, as AndroidAuto auto open even from locked screen. So it may be possible to do the same. I've looked up, but didn't find anything interesting at the moment. Commented Nov 26, 2024 at 14:48

1 Answer 1

0

Here's a github repo that explains how to start an app after boot up in Kotlin.

You may encounter issues on some recent Android versions and devices because of how OEM is managing battery optimization.

Sign up to request clarification or add additional context in comments.

1 Comment

I've tried several things but nothing has worked so far..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.