6

I am making a Flutter app which I want to make it start automatically on system boot. So far, I have tried autostart and flutter_boot_startup but these packages are outdated and they don't work (using Samsung Tab A, Android 10 - iOS is not important for me).

Is there any way to achieve this?

Teemo
  • 449
  • 6
  • 23

2 Answers2

1

Create a new java class in your android/src/main/java/<package-name>/.. folder (same folder as MainActivity.java)

Call it whatever you want e.g. BootBroadcastReceiver.java

package <your package name here>;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class BootBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            Intent i = new Intent(context, MainActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        }
    }
}

add this android permission to your AndroidManifest.xml

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

add this at the bottom of the <application ... /> object inside your AndroidManifest.xml

<receiver android:enabled="true" android:exported="false" 
android:name=".BootBroadcastReceiver"
            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>

The app needs to opened at least once before this will work.

this works for me on ATV devices and Acer/Lenovo tablets

Petr
  • 11
  • 1
0

AFAIK, one option that you can try is by pushing a local notification. You can consider using the package flutter_local_notifications. You could also check out this SO post which is related to your question.

What you can consider doing here is by pushing a local notification that when clicked, opens the app. App processes can be run in the background automatically by using auto_start_flutter and by requesting for the required permissions using getAutoStartPermission();. Note that this is currently only available for Android.

MαπμQμαπkγVπ.0
  • 5,887
  • 1
  • 27
  • 65