How can I check the current status of the GPS receiver in Android?


Introduction

There are many android applications which take location from the users for delivering any specific product or service at their doorstep. This application generally uses GPS to get the location of their user for delivering their service. To get the location from the GPS, we should check the current status of the GPS Receiver in our android device whether the GPS is on or off. In this article we will take a look on How can we check the current status of the GPS Receiver in Android.

Implementation

We will be creating a simple application in which we will be displaying a text view for heading, then we will be creating one more text view in which we will be displaying the current status of GPS whether it is on or off. Along with that we will be adding a button which we will be using to check the current status of GPS within our android application.

Step 1 : Creating a new project in Android Studio

Navigate to Android studio as shown in below screen. In the below screen click on New Project to create a new Android Studio Project.

After clicking on New Project you will get to see the below screen.

Inside this screen we have to simply select Empty Activity and click on Next. After clicking on next you will get to see the screen below.

Inside this screen we have to simply specify the project name. Then the package name will be generated automatically.

Note − Make sure to select the Language as Java.

After specifying all the details click on Finish to create a new Android studio project.

Once our project has been created we will get to see 2 files which are open i.e activity_main.xml and MainActivity.java file.

Step 3 : Working with activity_main.xml.

Navigate to activity_main.xml. If this file is not visible. To open this file. In the left pane navigate to app>res>layout>activity_main.xml to open this file. After opening this file. Add the below code to it. Comments are added in the code to get to know in detail.

<?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:id="@+id/idRLLayout"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   tools:context=".MainActivity">

   <!-- on below line creating a text view for displaying a heading-->
   <TextView
      android:id="@+id/idTVHeading"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_above="@id/idTVGPSStatus"
      android:layout_margin="10dp"
      android:gravity="center"
      android:text="Check Current Status of GPS Reciever in Android"
      android:textAlignment="center"
      android:textColor="@color/black"
      android:textSize="18sp"
      android:textStyle="bold" />

   <!-- on below line displaying a text to show GPS status-->
   <TextView
      android:id="@+id/idTVGPSStatus"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_centerInParent="true"
      android:layout_margin="10dp"
      android:gravity="center"
      android:text="GPS Status"
      android:textAlignment="center"
      android:textColor="@color/black"
      android:textSize="15sp"
      android:textStyle="normal" />

   <!-- on below line creating a button to check the GPS status -->
   <Button
      android:id="@+id/idBtnCheckGPS"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_below="@id/idTVGPSStatus"
      android:layout_margin="10dp"
      android:text="Check GPS Status"
      android:textAllCaps="false" />

</RelativeLayout>

Explanation − In the above code we are creating a Relative Layout as our root layout. Inside this relative layout we are displaying firstly our text view which will display the heading of our application. After that we are creating one more text view in which we will be displaying the current GPS status. Lastly we are creating a button which we will be using to check the current status of GPS.

Step 4 : Working with MainActivity.java file

Navigate to MainActivity.java. If this file is not visible. To open this file. In the left pane navigate to app>res>layout>MainActivity.java to open this file. After opening this file. Add the below code to it. Comments are added in the code to get to know in detail.

package com.example.androidjavaapp;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.location.GpsStatus;
import android.location.LocationManager;
import android.media.Image;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.internal.FlowLayout;

public class MainActivity extends AppCompatActivity {

   // on below line we are creating variable for text view.
   private TextView gpsStatusTV;
   private Button checkGPSStatusBtn;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      // on below line initializing variable with id.
      gpsStatusTV = findViewById(R.id.idTVGPSStatus);
      checkGPSStatusBtn = findViewById(R.id.idBtnCheckGPS);

      // on below line calling method to check GPS status
      checkGPSStatus();
      
      // on below line adding click listener for our button
      checkGPSStatusBtn.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            
            // on below line calling method to check gps status
            checkGPSStatus();
         }
      });
   }
   private void checkGPSStatus() {
      
      // Create a LocationManager instance
      LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
      
      // Check if GPS is enabled
      boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
      
      // If GPS is not enabled, show the user a dialog to enable it
      if (isGPSEnabled) {
      
         // Show the dialog
         gpsStatusTV.setText("GPS is Enabled..");
      } else {
         gpsStatusTV.setText("GPS is Disabled..");
      }
   }
}

Explanation − In the above code we are creating firstly the variable for our text view in which we have to display the GPS receiver status and a variable for the button which will be used to check the GPS status. After that we will get to see an onCreate method in which we are inflating the layout which we have to display to the users. Then we are initializing the variables for our button and text view with the id which we have given inside our activity_main.xml file.

After that we are creating a method checkGPS status which is used to check the current GPS status. Inside this method we are checking the GPS status and accordingly we are setting the GPS status to our text view. Now we are calling this method inside our onCreate method to display the GPS status. After that we are adding a click listener for our button to check the GPS status. Inside the click listener for this button we are again calling a method to check the GPS status to display the current GPS status.

After adding the above code now we have to simply click on the green icon in the top bar to run our application on a mobile device.

Note − Make sure you are connected to your real device or emulator.

Output

Conclusion

In the above article we have taken a look on How to get the current status of GPS receiver in our android application.

Updated on: 30-Mar-2023

701 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements