SensorService.java
Purpose
The SensorService is an Android Service class designed to run sensor-related background tasks in a persistent foreground mode. It primarily initializes and manages the MetaWearHandler to keep data streaming alive during app backgrounding.
Features & Responsibilities
Runs independently of Flutter UI as a long-lived background process.
Handles localization dynamically based on language settings passed via
Intent.Starts in the foreground with a persistent notification to prevent termination.
Verifies permission status (e.g.
ACCESS_FINE_LOCATION) on startup.
Initialization
@Override
public void onCreate() {
super.onCreate();
metaWearHandler = new MetaWearHandler(this, null);
}
Initializes the MetaWearHandler when the service is created.
Start Command Logic
@Override
public int onStartCommand(Intent intent, int flags, int startId)
Handles:
Language switching via
LANGUAGE_CODEintent extra.Permission check: If
ACCESS_FINE_LOCATIONis not granted, the service stops itself.Updates the app’s locale dynamically if the language is provided.
Foreground Notification
The method startForegroundServiceWithNotification():
Creates a
NotificationChannelBuilds and launches a persistent notification using
NotificationCompatPrevents Android from force-killing the background process
This is essential for uninterrupted sensor operations.
Language Localization
If LANGUAGE_CODE is provided via intent:
Locale locale = new Locale(languageCode);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.setLocale(locale);
Updates the application’s configuration to use the desired language.
Lifecycle Overrides
onBind()returnsnullas this is a started service.onDestroy()provides cleanup hooks (though none are implemented).
Integration
Designed to integrate tightly with
MetaWearHandler.javaUsed in scenarios where BLE sensors must stream data continuously even when the app is backgrounded.
Best Practices
Handles runtime permission validation safely.
Starts foreground notification for Android 8+ compliance.
Avoids binding; focuses on self-contained execution for background data acquisition.
Dependencies
MetaWearHandlerAndroid
Service,Notification,Locale, and BLE permission classes