Skip to content

This sample project demonstrates the setup for a foreground service. The service in this project is a foreground service used to request location. The required permissions depend on the type of foreground service.

Notifications You must be signed in to change notification settings

NicosNicolaou16/SampleForegroundServiceFlutter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sample Foreground Service Flutter

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.

Setup

Manifest Setup

<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 👈

Versioning

Flutter SDK version: 3.27.1
Dart Version: 3.6.0

References

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

About

This sample project demonstrates the setup for a foreground service. The service in this project is a foreground service used to request location. The required permissions depend on the type of foreground service.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published