How to Check GPS is On or Off in Android Programmatically?
Last Updated :
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 by Step Implementation
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
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.Context
import android.location.LocationManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import java.util.concurrent.Executors
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val mTextView = findViewById<TextView>(R.id.text_view)
val mButton = findViewById<Button>(R.id.button)
mButton.setOnClickListener {
val mLocationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
val mGPS = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
mTextView.text = mGPS.toString()
}
}
}
|
Output:
You can see that when the button is clicked, the status of GPS is displayed in the TextView.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...