Android Detect GPS Location Tutorial

Every android device has GPS feature to detect the device location. Using Location services can automatically get the current user location without his or her to enter the location in the mobile. With the location data, your app can predict a user’s potential actions, recommend actions, or perform actions in the background without user interaction. GPS feature able to get Latitude and Longitude of the device to determine where exactly the device locate. It is worth pointing out that Location can contain much more than just latitude and longitude values. In this tutorial, I will tell you how to use android detect GPS location in your project.

Creating a New Project

1. Open Android Studio IDE in your computer.
2. Create a new project and Edit the Application name to “GPSLocationExample”.
(Optional) You can edit the company domain or select the suitable location for current project tutorial. Then click next button to proceed.
3. Select Minimum SDK (API 15:Android 4.0.3 (IceCreamSandwich). I choose the API 15 because many android devices currently support more than API 15. Click Next button.
4. Choose “Empty Activity” and Click Next button
5. Lastly, press finish button.

Edit AndroidManifest.xml

Add 2 permissions in your manifest file to access your GPS location.

Create a new class

Right click your package name and create a class “GPSTracker” to manage location. Edit the file to the source code below.

Edit activity_main.xml layout

Add a button in the layout so that you can get the location by clicking the button.

Edit MainActivity.java class

Modify the MainActivity class to the following sample source code. By clicking the button, the app will get the location of the device.

Run Your Project

Finally, you can run it in your android device and make sure your GPS is open so it can get your device latitude and longitude.

(Android Detect GPS Location)

Source Code

(Visited 949 times, 1 visits today)
Advertisements

Yong Loon Ng

Ng Yong Loon, better known as Kristofer is a software engineer and computer scientist who doubles up as an entrepreneur.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *