You need to use permission first:

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

Next you inside onCreate() method use below code to get access to GPS.

Copy@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
            2000, 1, this);

}

Now if GPS is not enabled then you need to refer below code. For detailed explanation, you can refer how to get[DEAD LINK] [current location in android]1 tutorial.

CopyIntent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
         startActivity(intent);

Here we have used intents to redirect user to location settings page so that he can enable the GPS under location settings.

Answer from amit duhan on Stack Overflow
🌐
Android Developers
stuff.mit.edu › afs › sipb › project › android › docs › training › basics › location › locationmanager.html
Using the Location Manager | Android Developers
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); While not required, most modern Android-powered devices can receive location updates through multiple underlying technologies, which are abstracted to an application as LocationProvider objects.
🌐
Technobyte
technobyte.org › locationmanager-tutorial-android-studio
LocationManager Tutorial in Android
April 21, 2024 - In this tutorial, we will learn how to get the user’s location coordinates – Latitude, Longitude and Altitude by building a simple Android Application that will use the LocationManager class.
🌐
TutorialsPoint
tutorialspoint.com › android-gps-location-manager-tutorial
Android GPS, Location Manager tutorial
This example demonstrates how to access Android GPS, Location Manager Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
🌐
Android Developers
developer.android.com › api reference › locationmanager
LocationManager | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
🌐
Vogella
vogella.com › tutorials › AndroidLocationAPI › article.html
Android Location API with the fused location provider - Tutorial
February 26, 2026 - package de.vogella.android.locationsapi.simple; import android.app.Activity; import android.content.Context; import android.location.Criteria; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.widget.TextView; import android.widget.Toast; public class ShowLocationActivity extends Activity implements LocationListener { private TextView latituteField; private TextView longitudeField; private LocationManager locationManager; private String provider; /** Called when the activity is first created.
🌐
Codexpedia
codexpedia.com › home › android › android locationmanager code example
Android LocationManager Code Example - Codexpedia
December 7, 2015 - In Manifest.xml, add ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION permissions: [code language=”xml”] <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> [/code] In the Activity file, for example MainActivity.java, add the following to the onCreate method. It initializes the LocationManager, set the Criteria, get the location and add the location to the MyLocationListener.
Top answer
1 of 2
3

You need to use permission first:

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

Next you inside onCreate() method use below code to get access to GPS.

Copy@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
            2000, 1, this);

}

Now if GPS is not enabled then you need to refer below code. For detailed explanation, you can refer how to get[DEAD LINK] [current location in android]1 tutorial.

CopyIntent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
         startActivity(intent);

Here we have used intents to redirect user to location settings page so that he can enable the GPS under location settings.

2 of 2
1

you can achieve this easily. you have to write code in main activity. and Main activity implements Locationlistner interface. and overload its four methods. here i'am retrieving my Longitude and Latitide co-ordinates in a textview when application started. and you have to give permission for access location in your manifest file.

as under my code.

Copypackage com.nisarg.gpsdemo;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements LocationListener {

    private LocationManager locationManager;
 private TextView textview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textview=(TextView)findViewById(R.id.textView);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        Location location = locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER);

        onLocationChanged(location);
    }

    @Override
    public void onLocationChanged(Location location) {
        double longitude=location.getLongitude();
        double latitude=location.getLatitude();
        textview.setText("Longitude:   "+longitude+"   Latitide:  "+latitude);

    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {

    }

    @Override
    public void onProviderEnabled(String s) {

    }

    @Override
    public void onProviderDisabled(String s) {

    }
}

and you have to give permission like this in manifest file.

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

And last warning. you have to give permission to your application and start mobile data and GPS before you open application. either it will be crashed.

that's it. hope it will help you. :)

🌐
Medium
medium.com › @psarakisnick › android-location-manager-with-kotlin-flows-082c992d1b31
Android location manager with Kotlin flows | by Nick Psarakis | Medium
January 21, 2024 - Android location manager with Kotlin flows One of the most common device resources used in mobile applications is the current device location, lets see how we can get it in a clean way using Kotlin …
Find elsewhere
🌐
ProTech
protechtraining.com › blog › post › tutorial-android-location-service-example-198
Tutorial: Android Location Service Example - ProTech Training
April 28, 2011 - Hi folks, This tutorial will help you to start using location services (in particular: LocationManager class to get user location and Geocoder to translate location into addresses) and Google Maps on Android.
🌐
DigitalOcean
digitalocean.com › community › tutorials › android-location-api-tracking-gps
Android Location API to track your current location | DigitalOcean
August 3, 2022 - Depending on the conditions, this ... than GPS_PROVIDER · In this tutorial, we’ll create a Service that implements the LocationListener class to receive periodic location updates via GPS Providers or Network Providers....
🌐
TutorialsPoint
tutorialspoint.com › android › android_location_based_services.htm
Android - Location Based Services
package com.example.tutorialspoint7.myapplication; import android.app.AlertDialog; import android.app.Service; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.os.IBinder; import android.provider.Settings; import android.util.Log; public class GPSTracker extends Service implements LocationListener { private final Context mContext; // flag for GPS status boolean isGPSEnabled = false; //
🌐
Medium
medium.com › @boobalaninfo › accessing-users-location-guide-android-2023-60a6f018a718
Accessing User’s Location Guide Android 2023 | by Boobalan Munusamy | Medium
June 22, 2023 - // Create a LocationManager instance val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager // Request location updates from GPS provider locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0f, ...
🌐
Medium
medium.com › pickme-engineering-blog › building-a-modern-android-location-manager-from-legacy-approaches-to-clean-architecture-excellence-3e3e4590533e
Building a Modern Android Location Manager: From Legacy Approaches to Clean Architecture Excellence | by Chamod Lakmal | The PickMe Engineering Blog | Medium
September 16, 2025 - This setup ensures that the LocationManager instance is shared across the application while maintaining proper scoping for use cases. The FusedLocationManager implements the domain interface while handling all Android specific complexities.
🌐
Tabnine
tabnine.com › home › code library
Code Library - Tabnine
July 25, 2024 - Get the answers and suggestions you need from our AI code assistant. Get started in minutes with a free 90 day trial of Tabnine Pro.
🌐
The Crazy Programmer
thecrazyprogrammer.com › home › how to get current location in android using location manager
How to Get Current Location in Android Using Location Manager
January 11, 2017 - In this GPS tutorial you will learn how to get current location in android. LocationManager class provides the facility to get latitude and longitude coordinates of current location.
🌐
GitHub
github.com › yayaa › LocationManager
GitHub - yayaa/LocationManager: Simplify getting user's location for Android · GitHub
Simplify getting user's location for Android. Contribute to yayaa/LocationManager development by creating an account on GitHub.
Starred by 805 users
Forked by 186 users
Languages   Java
🌐
GitHub
github.com › grumpyshoe › android-module-locationmanager
GitHub - grumpyshoe/android-module-locationmanager: LocationManager Wrapper for Android · GitHub
LocationManager Wrapper for Android. Contribute to grumpyshoe/android-module-locationmanager development by creating an account on GitHub.
Starred by 21 users
Forked by 7 users
Languages   Kotlin
🌐
Android Developers
developer.android.com › api reference › location
Location | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
🌐
Dartmouth College
cs.dartmouth.edu › ~campbell › cs65 › lecture17 › lecture17.html
Location-Based Service –
public class WhereAmI extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); LocationManager locationManager; String svcName = Context.LOCATION_SERVICE; locationManager = (LocationManager)getSystemService(svcName); In the Manifest you will see that it is necessary to get the user's permission to track their location or get a location reading: <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />