Enable Disable GPS location service programmatically in android

How to Turn ON Turn OFF GPS inside android mobile phone using app on button click dynamically.

In this tutorial we are using intent to open GPS settings page dynamically on button click, So when application user click on button it will automatically open the GPS settings page so mobile user can itself enable and disable the GPS. So here is the complete step by step tutorial for Enable Disable GPS location service programmatically in android.

android-project-download-code-button

How to Enable Disable GPS location service programmatically in android.

Note : Please add below permission inside your AndroidManifest.xml file.

 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

Code for MainActivity.java file.

 package com.enabledisablegpslocationservice_android_examples.com;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

 Button button;
 Context context;
 Intent intent1, intent2;
 TextView textview;
 LocationManager locationManager ;
 boolean GpsStatus ;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 button = (Button)findViewById(R.id.button1);
 
 textview = (TextView)findViewById(R.id.textView1);
 
 context = getApplicationContext();
 
 CheckGpsStatus();
 
 button.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 intent1 = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
 startActivity(intent1);
 
 }
 });
 
 }
 
 public void CheckGpsStatus(){
 
 locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
 
 GpsStatus = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
 
 if(GpsStatus == true)
 {
 textview.setText("Location Services Is Enabled"); 
 }else {
 textview.setText("Location Services Is Disabled"); 
 }
 
 }
 
}

Code for activity_main.xml layout file.

 <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.enabledisablegpslocationservice_android_examples.com.MainActivity" >

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignTop="@+id/textView1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="70dp"
 android:text="Click Here to Enable Disable GPS location service programmatically in android" />

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="132dp"
 android:gravity="center"
 android:text="location service Stauts Shows Here"
 android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Code for AndroidManifest.xml file.

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.enabledisablegpslocationservice_android_examples.com"
 android:versionCode="1"
 android:versionName="1.0" >

 <uses-sdk
 android:minSdkVersion="14"
 android:targetSdkVersion="21" />
 
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

 <application
 android:allowBackup="true"
 android:icon="@drawable/ic_launcher"
 android:label="@string/app_name"
 android:theme="@style/AppTheme" >
 <activity
 android:name=".MainActivity"
 android:label="@string/app_name" >
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />

 <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
 </activity>
 </application>

</manifest>

Screenshots:

on-gps-1

on-gps-2

Enable Disable GPS location service programmatically in android

Click here to download Enable Disable GPS location service programmatically in android

2 Comments

  1. It executes Successfully.Thank You.

Leave a Reply

Your email address will not be published. Required fields are marked *