This sample project demonstrates the setup for a foreground service, specifically for requesting
location data. The permission <uses-permission android:name=" android.permission.FOREGROUND_SERVICE_LOCATION" />
depends on the type of foreground service being
used.
Communication between Android Native and Flutter is handled via a channel (refer to the
documentation for more details). If you need additional examples on using channels, please check my
repository for more information.
ExampleChannelNativeAndroidWithCustomData
Important
NOTE: EXAMPLE FOR ANDROID ONLY.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<!--Location Permissions-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<!--Foreground Service Permissions-->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />
<!--Restart Service After Reboot the Mobile Device-->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application>
<!--other code here-->
<!--Service-->
<service android:name=".service.LocationService" android:enabled="true"
android:exported="false" android:foregroundServiceType="location" />
<!--Broadcast Receiver (Optional)-->
<receiver android:name=".broadcast_receiver.RestartServiceBroadcastReceiver"
android:enabled="true" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<!--other code here-->
</application>
</manifest>
Important
Check my article for the setup 👉 Foreground Service in Android - Medium 👈
Important
Similar project with (Kotlin Language) 👉 SampleForegroundService 👈
Flutter SDK version: 3.27.1
Dart Version: 3.6.0
https://developer.android.com/about/versions/14/changes/fgs-types-required
https://stackoverflow.com/questions/14385231/android-broadcastreceiver-auto-run-service-after-reboot-of-device
https://medium.com/@shreebhagwat94/flutter-platform-channel-46578d21e69
https://blog.devgenius.io/use-flutter-screens-in-native-android-app-share-data-among-them-d97d670807a6