Click here to Skip to main content
15,884,176 members
Articles / Mobile Apps / Android
Tip/Trick

Check whether location services are enabled on Android Phone

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
1 Apr 2013CPOL 18.9K   3   1
Checking location services are enabled or disabled on android phone.

Introduction

Nowadays, Many applications use location services. As a developer, you have to check location services are enabled or disabled if your app uses location services. Let's look at how to check location services. 

Using the code 

First, you have to add location services permission to manifest file. 

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

Second, you have to check whether location services are enabled by using some boolean function. To do this, you have to access to location manager. Location manager has some provider information. For example location services are enabled or disabled. 

 public static boolean control(){

LocationManager locManager = (LocationManager) getSystemService(LOCATION_SERVICE);  

        if (locManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){  
           //GPS enabled
           return true;
        }
           
        else{
           //GPS disabled
           return false;
        }
}       

 As you see, if location services are enabled, function returns true. If it isn't, function returns false.  

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer NoMad Commerce
Turkey Turkey
Melih Mucuk

Software & Mobile App Developer

Visit my blog: http://melihmucuk.com

Comments and Discussions

 
QuestionMessage Closed Pin
10-Oct-13 22:24
kasun61810-Oct-13 22:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.