How to Check GPS is On or Off in Android Programmatically?

27 Sep, 2021
GPS (Global Positioning System) is a satellite-based navigation system that accommodates radio signals between satellite and device to process the device’s location in the form of coordinates. GPS gives latitude and longitude values of the device. Recent mobile phones are equipped with GPS modules to know their exact location on software like Google Maps. However, GPS data is used for many other applications for finding the device, showing news related to that geographic location, and many others.
This article will show you how you could programmatically check if the GPS in an Android device is enabled or disabled.
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: Add permissions in the AndroidManifest.xml
XML
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Step 3: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Add a TextView and a Button in the layout file. The TextView will display if the GPS is enabled or disabled upon the Button trigger.
XML
<?xml version="1.0" encoding="utf-8"?><RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity">      <TextView        android:id="@+id/text_view"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:text="Hello Geek!"        android:layout_above="@+id/button"/>      <Button        android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:text="Click"/>  </RelativeLayout>
Step 4: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
import android.content.Contextimport android.location.LocationManagerimport androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport android.os.Handlerimport android.os.Looperimport android.widget.Buttonimport android.widget.TextViewimport android.widget.Toastimport java.util.concurrent.Executors  class MainActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)          // Declaring TextView and Button from the layout file        val mTextView = findViewById<TextView>(R.id.text_view)        val mButton = findViewById<Button>(R.id.button)          // What happens when button is clicked        mButton.setOnClickListener {              // Calling Location Manager            val mLocationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager                            // Checking GPS is enabled              val mGPS = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)                            // Display the message into the string              mTextView.text = mGPS.toString()        }      }}
Output:
You can see that when the button is clicked, the status of GPS is displayed in the TextView.
Android
Kotlin
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.