flutter_boot_receiver
A Flutter plugin for registering Dart callbacks when the Android device boots up.
Contents
Platform Support
Platform | Supported |
---|---|
Android | ✔️ Yes |
iOS | ❌ No |
Web | ❌ No |
Windows | ❌ No |
Linux | ❌ No |
MacOS | ❌ No |
Installation
flutter pub add flutter_boot_receiver
In your AndroidManifest.xml
:
- Add
android:installLocation="internalOnly"
in yourmanifest
tag. Only devices installed in internal storage can receive theBOOT_COMPLETE
broadcast in Android.
<manifest
android:installLocation="internalOnly"
>
- Add the following permission:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
- Add the following service and receiver inside your
application
tag:
<service
android:name="com.flux.flutter_boot_receiver.BootHandlerService"
android:exported="false"
android:permission="android.permission.BIND_JOB_SERVICE"
/>
<receiver
android:enabled="true"
android:exported="true"
android:name="com.flux.flutter_boot_receiver.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>
Your AndroidManifest.xml
should now have a structure similar to this:
<manifest
android:installLocation="internalOnly"
>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<!-- Other permissions... -->
<application>
<!-- Other activities, services etc... -->
<service
android:name="com.flux.flutter_boot_receiver.BootHandlerService"
android:exported="false"
android:permission="android.permission.BIND_JOB_SERVICE"
/>
<receiver
android:enabled="true"
android:exported="true"
android:name="com.flux.flutter_boot_receiver.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>
</application>
</manifest>
Usage
import 'package:flutter_boot_receiver/flutter_boot_receiver.dart';
@pragma('vm:entry-point')
void callback() async {
// Code here will be executed when the device boots up
}
await BootReceiver.initialize(callback);