Flutter plugin that surfaces real-time Android network throughput using TrafficStats, with optional foreground-service monitoring and notification updates.
- TrafficStats-based download/upload/total bytes-per-second sampling
- Stream-based updates with strongly typed models
- Configurable interval (default 1000ms) and basic validation
- Optional foreground service with ongoing notification updates
- Formatting helpers for B/s, KB/s, MB/s, GB/s
- Android 14 foreground service type (
dataSync) declaration
Android only. iOS/web/desktop are not implemented.
Add to your pubspec.yaml:
dependencies:
network_speed_meter: ^0.1.0- Minimum SDK: 21 (Lollipop). TrafficStats is available earlier; 21 is a modern floor and matches current Flutter defaults.
- Compile/target SDK: 35 (Android 15 ready).
- Required permissions in your app manifest (plugin declares them, but you may prefer to own them):
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" /> <uses-permission android:name="android.permission.POST_NOTIFICATIONS" tools:targetApi="33" />
- Foreground service entry (already included in the plugin manifest):
<service android:name="com.networkspeedmeter.network_speed_meter.NetworkSpeedForegroundService" android:exported="false" android:foregroundServiceType="dataSync" />
- Android 13+: request
POST_NOTIFICATIONSat runtime before enabling foreground notifications. - Android 14+: verify your use of
FOREGROUND_SERVICE_DATA_SYNCcomplies with Play policies.
import 'package:network_speed_meter/network_speed_meter.dart';
Future<void> start() async {
const config = NetworkSpeedConfig(
interval: Duration(seconds: 1),
enableForegroundService: false,
showNotification: true,
notificationTitle: 'Network speed',
notificationContent: 'Monitoring traffic...',
);
await networkSpeedMeter.startMonitoring(config: config);
networkSpeedMeter.speedStream.listen((snapshot) {
print('Down: ${snapshot.formattedDownload}, '
'Up: ${snapshot.formattedUpload}, '
'Total: ${snapshot.formattedTotal}');
});
}
Future<void> stop() => networkSpeedMeter.stopMonitoring();Enable enableForegroundService in NetworkSpeedConfig to keep monitoring in the background with an ongoing notification. The service:
- Uses foreground service type
dataSync - Updates the notification with current speeds
- Broadcasts updates back to Flutter via the event channel
SpeedFormatter converts byte-per-second values into readable strings:
speedFormatter.format(1024); // "1.0 KB/s"
speedFormatter.format(5 * 1024 * 1024); // "5.0 MB/s"example/lib/main.dart mirrors the Play Store reference app with four tabs:
- Dashboard: live download/upload/total cards, chart, controls, session stats
- Usage: daily counters, session usage, peaks, notification preview
- History: per-session archive with averages and peaks
- Settings: interval, auto-start, foreground/notification toggles and demo reset
Run it from example/ with flutter run -d android.
- Dart unit tests:
flutter test(formatter, config validation, platform wiring) - Android instrumentation scaffold:
android/src/androidTest/...contains a smoke test placeholder; extend with TrafficStats monitor tests using an emulator.
- Start monitoring in app, observe steady stream updates
- Validate first sample does not spike (baseline is skipped)
- Stop monitoring: stream stops and state resets
- Toggle foreground mode: notification appears with speeds, background updates continue
- Android 13+: request notification permission and confirm notification shows
- Android 14+: verify service starts with
dataSynctype and no policy warnings
- TrafficStats reports per-device counters; OEM status-bar values may differ because of smoothing/rounding.
- Values represent throughput since the previous sample, not latency or server-side speed tests.
- Foreground service behavior depends on OEM policies and battery optimizations; document requirements for your app.
- Plugin is Android-only; other platforms throw unimplemented errors.
- Rebuilt as a Flutter plugin (not a standalone Android app) with MethodChannel for control and EventChannel for streaming.
- Kotlin-based monitor and optional foreground service with structured callbacks instead of UI-bound logic.
- Strongly typed Dart API/models, formatting helpers, and unit tests for core utilities.
- Configurable interval and notification metadata provided from Flutter rather than hard-coded.
- Conceptually informed by TrafficStats-based monitoring in https://github.com/thanush0/InternetSpeedMeter, but fully refactored into plugin architecture with new code.
- Create/update dependencies:
flutter pub get - Run tests:
flutter test - Run example (Android):
flutter run -d androidfromexample/ - Build debug APK:
flutter build apk --debugfromexample/(automatically signed, can be installed) - Build release APK: Configure signing in
example/android/app/build.gradlefirst, thenflutter build apk --release
The example app can be built as an APK for installation on Android devices:
Debug APKs are automatically signed with a debug keystore and can be installed directly:
cd example
flutter build apk --debugThe APK will be at example/build/app/outputs/flutter-apk/app-debug.apk.
Release APKs require signing configuration. Add to example/android/app/build.gradle:
android {
signingConfigs {
release {
storeFile file(System.getenv("KEYSTORE_FILE") ?: "release-keystore.jks")
storePassword System.getenv("KEYSTORE_PASSWORD")
keyAlias System.getenv("KEY_ALIAS")
keyPassword System.getenv("KEY_PASSWORD")
}
}
buildTypes {
release {
signingConfig signingConfigs.release
}
}
}Then build:
cd example
flutter build apk --releaseNote: The GitHub Actions workflow builds debug APKs to ensure they can be installed without additional signing setup.