Coders Packet

Get User Location in Android Studio in Java

By Amit Kumar

In this project, I have created an Android app that returns the latitude, longitude and location of the user and thus detects the user location.

In this project, I have created an Android app that returns the user's latitude and longitude and by locating the user's location from the same latitude and longitude, we have that location printed on the screen of the app.

For this project, I requested permission from the user to use their location. From Android 6.0(Marshmallow), the concept of run-time permissions was rolled in and so the same will be used for getting permission.

I have used both of these in the Manifest file for user permission.

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

After this, I check if the permissions we requested are enabled or not, If not then we request permissions again. After that, I got the user's location with the help of predefined classes LocationManager and LocationListener and printed that location on the screen of the app.

Note: If the app does not show the location for the first time, close the app and reopen it.

Download Complete Code

Comments

No comments yet