7

I want to start my app automatically at boot on Android devices.

Is there any package or any solution for doing this? I know how to do it in native Android code, but how can it be done in Flutter?

holocronweaver
  • 2,171
  • 3
  • 18
  • 20
appdev
  • 199
  • 3
  • 14
  • 1
    You can follow this link: https://stackoverflow.com/a/6392009/13109852 – Salih Can Feb 03 '22 at 05:40
  • exactly but how to do that in flutter – appdev Feb 03 '22 at 05:42
  • Actually you can do it exactly as in the answer. You just have to change "com.myapp.MyService" to your own. Finally, you should update the name "MyService.class" to "MyActivity.class". – Salih Can Feb 03 '22 at 06:04
  • 'onReceive' hides member of supertype 'BroadcastReceiver' and needs 'override' modifier. getting this error – appdev Feb 03 '22 at 07:45
  • You should add @Override annotation top of onReceive method, if you already added you could add more code in your post. – Salih Can Feb 03 '22 at 11:20
  • yeah that error is gone now ....the app runs fine... but i dont know why but it is not working – appdev Feb 04 '22 at 04:17
  • Check my answer below, maybe an issue with `autostart` restrictions – Chris Jun 23 '22 at 16:42

2 Answers2

4

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:name=".BootBroadcastReceiver" >   
    <intent-filter>   
        <action android:name="android.intent.action.BOOT_COMPLETED" />  
        <action android:name="android.intent.action.QUICKBOOT_POWERON" /> 
    </intent-filter>   
</receiver> 

The name of the <receiver .. /> should match the name of the class. Just copy above.

I've found certain devices, such as Xiaomi, have a security feature to prevent "autostart". This can be enabled/disabled for an app in the Security app, or search for "autostart" in the Settings and you'll be taking to the correct section.

I think the app needs to opened at least once before this will work.

Chris
  • 1,720
  • 16
  • 35
  • If i want start socket, how this code work? – Rian Pratama Jun 07 '23 at 07:30
  • I'm not sure what you mean. You should be able to see in the `BootBroadcastReceiver` that inside of `onReceive` it starts a new `Intent`. In this case it starts `MainActivity.class`, which will essentially begin running the app in the background. In summary, when the device is booted (turned on), and it has finished initialising whatever it needs to do, it will then send a message to all BootBroadcastReceivers that the device has finished turning on. – Chris Jun 08 '23 at 06:41
  • Can you help me auto start socket on backround? I'm was try one more package foreground and backround service, but notification always show – Rian Pratama Jun 10 '23 at 15:49
0

You can just use the auto_start_flutter package.

https://pub.dev/packages/auto_start_flutter

Gedeon Mutshipayi
  • 2,871
  • 3
  • 21
  • 42