Android GPS location

If your application needs to get the current location of an Android device, this tip is useful to you. In this Android tip, I am going to show you how to achieve this goal using GPS location service provider.
Using GSP location service provider requires the ACCESS_FINE_LOCATION permission. So you have to add the following line to your AndroidManifest.xml file above the <application> tag.

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

Next, in your code, you need to get LocationManager instance using the getSystemService() with passing Context.LOCATION_SERVICE. Then you register the LocationManager instance to receive the location updates using the requestLocationUpdates() method. With the requestLocationUpdates() method, you will pass a location provider, minimum time interval between location updates, in milliseconds, minimum distance between location updates, in meters, and a LocationListener. In this case the location provider is LocationManager.GPS_PROVIDER. You must override onLocationChanged() of the LocationListener to get the current location updates. The example code below gets the current location from and Android device and display the latitude and longitude of the location in a TextView.

public class MainActivity extends Activity{
TextView tv;
   protected void onCreate(Bundle savedInstanceState) {
     // Get LocationManager instance
     LocationManager locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
     // Register the location manager to receive location updates
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,new MyCurrentLoctionListener());
   public class MyCurrentLoctionListener implements android.location.LocationListener {
   public void onLocationChanged(Location location) {
   // Display the current location
   String curLocation = "Latitude = " + location.getLatitude() + "\r\nLongitude = " + location.getLongitude();
   public void onProviderEnabled(String s) {
   public void onProviderDisabled(String s) {
   public void onStatusChanged(String s, int i, Bundle bundle) {


Posted by: Dara | post date: 01-12-2015 | Subject: Android Apps Development

write programming tip

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.