Kotlin how to check GPS service is enabled or disabled example

In this example, I will show you how to check GPS is enable or disable & how to open GPS enable screen.

Kotlin how to check GPS service is enabled or disabled example video

  • You can also go through this video explanation.

Kotlin how to check GPS service is enabled or disabled example

Creating a New Project

  • So first create a new Android Studio project using Kotlin.
  • Once the project is loaded come inside the activity_main.xml and remove the Hello World TextView.
Android studio project screen

Step 1:
Open AndroidManifest.xml and add Internet Permission.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Step 2:
Create new Class:- CommonClass.kt
CommonClass.kt
Create new class CommonClass.kt and Add following code.
package com.kotlinkatta.gpsenable

import android.content.Context
import android.content.Intent
import android.location.LocationManager
import android.provider.Settings

class CommonClass {

fun checkGpsStatus(context: Context): Boolean {
lateinit var locationManager: LocationManager
var gpsStatus = false
locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
gpsStatus = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
if (gpsStatus) {
return true
} else {
return false
}
}

fun open_Gps_Screen(context: Context) {
val intent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(intent);
}
}
Step 3:
MainActivity.kt
Add following code into MainActivity.kt
package com.kotlinkatta.gpsenable

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView

class MainActivity : AppCompatActivity() {
lateinit var txt_response: TextView
lateinit var btn_enable_gps: Button

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

txt_response = findViewById<TextView>(R.id.txt_response)
func_Check_Gps_Status()

btn_enable_gps = findViewById<Button>(R.id.btn_enable_gps)
btn_enable_gps.setOnClickListener {
CommonClass().open_Gps_Screen(applicationContext)
func_Check_Gps_Status();
}
}

private fun func_Check_Gps_Status() {
if (CommonClass().checkGpsStatus(applicationContext) === true) {
txt_response.setText("GPS Enable")
} else {
txt_response.setText("GPS Disable")
}
}

override fun onResume() {
super.onResume()
func_Check_Gps_Status()
}
}
Step 1:
activity_main.xml
Add following code into activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<Button
android:id="@+id/btn_enable_gps"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="Click To Open GPS Enable/Disable Screen"
android:layout_gravity="center"
android:textAllCaps="false"/>

<TextView
android:layout_marginTop="10dp"
android:id="@+id/txt_response"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:layout_gravity="center"
android:gravity="center"
android:textSize="20dp"/>

</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
OutPut:



Comments