How to programmatically check android mobile phone GPS running status or not using app on button click.
Location service means GPS( Global Positioning System ) service. This service is most important for android users because with this they can easily locate their locations from any where any place. By default GPS is disabled in android mobile phones and if you are going to design any application which needs GPS coordinates then this the first step from where you can determine the location service is running or no in background. So here is the complete step by step tutorial for Check GPS location services is enabled or not in Android.
How to Check GPS location services is enabled or not in Android.
Note : Please add ACCESS_FINE_LOCATION permission to your AndroidManifest.xml file.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Code for MainActivity.java file.
package com.checkgpslocationservicesisenabled_android_examples.com; import android.app.Activity; import android.content.Context; import android.location.LocationManager; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { Button button; TextView textview; Context context; 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(); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub CheckGpsStatus() ; if(GpsStatus == true) { textview.setText("Location Services Is Enabled"); }else { textview.setText("Location Services Is Disabled"); } } }); } public void CheckGpsStatus(){ locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE); GpsStatus = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); } }
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.checkgpslocationservicesisenabled_android_examples.com.MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="Click Here to Check GPS location services is enabled or not in Android" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/button1" android:layout_centerHorizontal="true" android:layout_marginBottom="48dp" android:text="GPS Status" android:textAppearance="?android:attr/textAppearanceLarge" android:gravity="center" /> </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.checkgpslocationservicesisenabled_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>
Screenshot: