Autostart on BOOT_COMPLETED for flutter application based on Android example not working

6,215

Thank to very much to Mike M. His suggestion and pointing at the other android based discussion gave me enough info to archive autostart on boot. The code change to the above example follows..

In the StartCmPlayerServiceAtBootReceiver class, Change to

public void onReceive(Context context, Intent intent) {
    if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
        Intent mIntent = new Intent(context, MainActivity.class);
        mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(mIntent);
    }
}

Thanks again, and I hope other flutter devs find this useful.

Share:
6,215
James Gardiner
Author by

James Gardiner

Updated on December 08, 2022

Comments

  • James Gardiner
    James Gardiner over 1 year

    There are quite a few examples of using BOOT_COMPLETED to start an application when the device boots.. I have attempted to use these example against my Flutter application. Having it start the App. This is for a simply signage app that shows images. Basically similar to a picture frame.

    In the example code below, the application is compiling, however, when I reboot a simulator, for example, the code does not appear to have any effect.

    My guess is that I am not calling the right code to actually start the application.. I am not a Android developer, so am having issues figuring what is exactly going on. Manifest follows..

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="au.net.digitall.cmplayer">
    
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    
    <application
        android:name="io.flutter.app.FlutterApplication"
        android:label="cm_player"
        android:icon="@mipmap/ic_launcher"
        tools:ignore="GoogleAppIndexingWarning">
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/cmTheme2"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            
            <meta-data
                android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
                android:value="true" />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    
        <receiver
            android:enabled="true"
            android:name=".StartCmPlayerServiceAtBootReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    </application>
    

    Then the StartCmPlayerServiceAtBootReceiver class to start the APP..

    package au.net.digitall.cmplayer;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.util.Log;
    
    
    public class StartCmPlayerServiceAtBootReceiver extends BroadcastReceiver {
    
        private static final String TAG = StartCmPlayerServiceAtBootReceiver.class.getSimpleName();
    
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i(TAG, "BOOT detected");
            if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
                Intent serviceIntent = new Intent(context, MainActivity.class);
                context.startService(serviceIntent);
            }
        }
    }
    

    This all compiles and runs, but nothing happens on reboot. Appreciate the help..

  • bluenile
    bluenile about 5 years
    In which file do we add this code in the flutter app?
  • Michael Tolsma
    Michael Tolsma over 3 years
    I still get no result when using the above code. I get zero errors, but restarting the emulator does nothing.
  • micmor
    micmor about 3 years
    In this example you have to add this Code into a StartCmPlayerServiceAtBootReceiver.java file next to the Mainactivity.java file .
  • Edemilson Lima
    Edemilson Lima almost 3 years
    I have only a MainActivity.kt at the Kotlin folder. Will this java file work there too?
  • Sipke Schoorstra
    Sipke Schoorstra over 2 years
    At the same level of the Kotlin folder, there is a Java folder.
  • bulgarian-beast
    bulgarian-beast almost 2 years
    Hello where to add the 4 parts, the application does not start
  • Miftakhul Arzak
    Miftakhul Arzak almost 2 years
    @bulgarian-beast, add the code on MainActivity.kt. You can find it on project_name/android/app/src/main/kotlin/com/example/app/