Welcome to Part 1 of this review of the Pluralsight course Android Location-Based Apps by Jim Wilson
Jim has over 30 years of software engineering experience, with the past 15 years heavily focused on creating mobile device and location-based solutions. He has co-founded multiple software-related startups and has served in a consulting role at several more.
After nearly a decade as a Microsoft Device Application Development MVP, Jim now focuses on developing Android and iOS device applications. Jim’s passion is mentoring software developers. He blogs at hedgehogjim.wordpress.com.
Android Location-Based Apps
Android Location Basics
Accessing the location system
The LocationManager is the root of the location system, and it provides hooks into all location capabilities.
Jim says the LocationManager and the location system is a really powerful mechanism.
He explains how to access the LocationManager.
Location providers and security
We learn that most Android devices support 2 location providers: Global Positioning System (GPS), and Network-based.
Jim warns that although GPS is pretty accurate, it’s also quite power intensive, so it can wear down the users’ batteries if overused.
We learn that the Network-based location provider uses Wi-Fi hotspots and cellular towers.
Google store all the wi-fi signals that they see and where they saw them, in a big database. Also in this database is a list of all the cellular towers and their locations.
Network-based is not as accurate as GPS, and if it’s based on cellular tower information it’s not nearly as accurate. But it’s a lot less power intensive, and can work indoors as well as outdoors.
Also discussed in this lesson is location security. Your application must request permission to use the location service, and there are different permissions for each location provider.
Network based location requires the ACCESS_COURSE_LOCATION permission.
GPS based location requires the ACCESS_FINE_LOCATION permission.
Handling location information
Jim teaches us about the LocationListener interface, and we learn that we create our own class that implements this.
Jim discusses 4 main methods:
It’s important to remember that your user can turn off GPS in the middle of using your application. It is best to design your application to detect when that happens and deal with it gracefully, and some of these methods help you with that.
Jim also discusses the Location class, and this class provides raw location information.
The Location class has a lot of different methods. Jim describes the most commonly used ones:
He also mentions the names of many others. See the Android API reference for full details.
If you don’t have much experience with date and times, I highly recommend the Date and Time Fundamentals course. For more information on that see my Top 10 Pluralsight Courses article.
Demo: Handling location information
Jim uses the JetBrains IntelliJ IDEA for this demo. Android Studio is based on this. To learn more about Android Studio see Exploring Android Studio.
Jim creates MyLocationListener.java and makes the class implement LocationListener, and implements all four of the methods mentioned above.
We see Jim write and explains the code required for each of these methods. In this example, for simplicity we are only logging the information.
Receiving location updates
We learn that the steps required to receive location updates is straight forward:
- Request a reference to the location service
- Create instance of LocationListener implementation
- Call the location service’s requestLocationUpdates method
- Updates continue until removeUpdates is called
Demo: Receiving location updates
Jim demonstrates the process described in the previous clip.
This involves both a network listener and a gps listener, and requires updating our AndroidManifest.xml adding
<uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION” />
Getting a single location value
Jim explains that there are two ways to do this:
Which you choose will depend on whether you want speed or accuracy.
Jim warns that in some cases he’s seen last known locations that were many days old. We also need to be aware that we could get null returned.
Demo: Getting a single location value
Jim demonstrates the process described in the previous clip. Again we are just logging out the values.
We use the primary thread here, but Jim says we’ll be talking a lot more about threading in a later module.
Continue to Part 2 – Android Location Providers