How to check GPS is currently enabled or disabled in your android mobile programatically

15/04/2017   03:43:27 PM

Hello in this android article i explain how to create an android application to check GPS status enabled or disabled of your android mobile.

GPS is Global Positioning System.It is navigation system based on satellite.

 

1. Create a new project in android studio.

2. Add following code on activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.xperiaitinnovations.gpscheck.MainActivity">
</RelativeLayout>

 

3 .Add following code in MainActivity.java

import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
 
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String checkcurrentgps= Settings.Secure.getString(getContentResolver(),Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(checkcurrentgps.equals("")){ Toast.makeText(MainActivity.this, "GPS Disable", Toast.LENGTH_SHORT).show();}
 
        else
{
 
    Toast.makeText(MainActivity.this, "GPS Enable", Toast.LENGTH_SHORT).show();
}
    }
}

 

OUTPUT

android application to check GPS status enabled or disabled


Output

                   

Support us by Sharing