Reputation: 289
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?
Upvotes: 10
Views: 8809
Reputation: 4073
You can just use the auto_start_flutter
package.
https://pub.dev/packages/auto_start_flutter
Upvotes: 0
Reputation: 2034
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.
Upvotes: 6