Android 14+ Security Hardening
I have implemented critical security fixes to prevent SecurityException crashes on Android 14+ (API 34) and higher, ensuring a stable experience during the first run and background startup.
Technical Fixes for Modern Android
1. Permission-Aware Service Startup
Android 14 introduced strict requirements for starting Foreground Services (FGS) from the background. We addressed this with:
- Startup Guard: Removed the immediate service start in
MainActivity#onCreate. The monitor now only attempts to start once permissions are confirmed or when the user saves their settings. - Strict Location Check: In [EmergencyService.java], the app now verifies that both
ACCESS_FINE_LOCATIONandACCESS_BACKGROUND_LOCATIONare granted before declaring thelocationservice type.
2. SecurityException Prevention (Graceful Fallback)
If the service is triggered (e.g., via a boot sequence) but the user has not yet granted the "Always Allow" location permission:
- Fallback Mode: The service starts as a standard foreground service without the
locationcapability. This prevents the app from crashing. - Safety First: The app logs a warning indicating that the SOS alert will lack GPS coordinates until the required permission is granted, rather than failing catastrophically.
3. Race Condition Resolution
Fixed a race condition where the app would attempt to start the monitoring service while the permission dialog was still visible to the user, which is now a fatal error in the latest Android versions.
Verification Summary
Manual Reliability Check
- Fresh Install: Verified that the app no longer crashes upon the first launch.
- Permission Denial: Verified that the app stays running even if location permission is temporarily unavailable, avoiding the
SecurityException. - Android 14/15/16 Support: The
startForegroundlogic now uses the exact bitwise flags required by the newest Android Target SDK (36).
Nugon SOS is now fully compatible with the latest Android security enforcements.