In our last project, we worked on a smartphone app to combat electric vehicle anxiety by understanding users’ needs, providing the owner with real-time data about their drives and offering a mechanism to compare suitable electric vehicles. In order to make that happen, tracking of driving behaviour through the app was an essential aspect of the project. It means that the app should be able to constantly listen to location coordinates in the background during the user’s journey while driving.
We had to implement checks to ensure GPS is enabled on the device and Location Permission is granted by the user. It is important to highlight that both are critically needed to ensure the app can perform Location Tracking during all drives.
Use Case 🚙
Imagine a scenario when the user installs the app, goes through onboarding and maybe grants Location Permission but ignores the dialog which prompts to enable GPS. Even worse, the user didn’t grant permission access or revoked later for any reason. Now it is safe to assume that user will even forget about the newly installed app and intended goal won’t be achieved.
Updated Use Case 🚙
Imagine a scenario when the user installs the app, goes through onboarding and grants Location Permission and enables GPS when prompted. There is still a chance that the user can revoke permission at any time in future for any reason. Even worse, the user started driving but don’t have GPS enabled. Those drives will never be tracked and the app will not be able to perform its intended goal.
Our best bet was to inform visually when the app is in the foreground or background in order to encourage the user to fulfil one or both of these requirements. This was only possible when UI (Activity/Fragment) and Service can listen to changes in GPS and Runtime Permission and react accordingly.
I faced several challenges while doing that and decided to share my learnings and solution. It is not the best solution ever but something which works for now and I would be more than happy and thankful to hear feedback on doing it in a better way ✌️
Sample App UI
Before I move to implementation part, I would like to mention that our team already decided to use Architecture Components so it was a natural choice to opt for LiveData to implement GPS and Permission Listeners so to say.
In the first part of this tutorial, we can see how to implement checks using LiveData and observe on UI level.
In the second part of this tutorial, we can see how to implement checks using LiveData and observe on the background Service level.
You can have a look at the code on GitHub.