Sunday, March 6, 2016

Get Current Location In Activity In Android

This Android tutorial is to assist discover the place centered solution in the Android system. Understanding the present place in an android mobile will pave the method for establishing many ingenious Android applications to resolve individuals everyday issue. For establishing place conscious application in android, this requires place service providers. There‘re 2 kinds of place service providers,

  • GPS Place Provider
  • Network Place Provider

Any among the over service providers suffices to obtain present place from the individual or user’s gadget. However, it‘s suggested to use both service providers as they both have various benefits. Because the GPS service provider will take some time to obtain the place at the interior location. And, the Network Place Service provider will not get a place when the network connection is bad.

Get Current Location in Android

Network Place Service provider vs GPS Place Provider 

  1. Network Place service provider is relatively quick compared to the GPS service provider in offering the plane coordinates. 
  2. GPS service provider might be really very slow down in in-door places and will drainpipe the mobile battery. 
  3. Network place service provider depends upon the cell loom and will return our closest loom place. 
  4. A GPS place service provider will provide our place precisely. 

Actions to obtain the place in Android 

  1. Offer consents in show submit for getting place update 
  2. Produce LocationManager circumstances as a recommendation to the place service 
  3. Ask for a place from LocationManager 
  4. Get place upgrade from LocationListener on modification from location 

Offer consents for getting place update 

To gain access to present place info with place service providers, we have to established consents with android manifest submit.

<manifest ... >
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission. ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
ACCESS_COARSE_LOCATION is utilized when we usage network place service provider for our Android application. However, ACCESS_FINE_LOCATION is offering consent for both service providers. INTERNET consent is should for using network service provider.

Produce LocationManager circumstances as the recommendation to the place service

For any history Android Solution, we require to obtain a recommendation for utilizing this. Likewise, place solution recommendation will be produced utilizing getSystemService () technique. This recommendation will be included with the recently produced LocationManager circumstances as complies with.
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

Ask for a present place from LocationManager 

After producing the place solution recommendation, place updates are asked for utilizing requestLocationUpdates () technique from LocationManager. For this feature, we have to send out the kind of place service provider, a variety of secs, range and the LocationListener item over which the place to be upgraded.
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

Get place upgrade from LocationListener on modification from location 

LocationListener will be informed based upon the range period defined or the number secs.

Example Android Application : Current Place Finder 

This instance offers a present place upgrade utilizing GPS service provider. Whole Android application code is as complies with,
import android.os.Bundle;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.widget.TextView;
import android.util.Log;
public class MainActivity extends Activity implements LocationListener{
protected LocationManager locationManager;
protected LocationListener locationListener;
protected Context context;
TextView txtLat;
String lat;
String provider;
protected String latitude,longitude;
protected boolean gps_enabled,network_enabled;
protected void onCreate(Bundle savedInstanceState) {
txtLat = (TextView) findViewById(;
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
public void onLocationChanged(Location location) {
txtLat = (TextView) findViewById(;
txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude());
public void onProviderDisabled(String provider) {
public void onProviderEnabled(String provider) {
public void onStatusChanged(String provider, int status, Bundle extras) {
XML files for layout and android manifest are as shown below
<RelativeLayout xmlns:android=""
tools:context=".MainActivity" >
android:text="@string/hello_world" />
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
android:versionName="1.0" >
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
android:theme="@style/AppBaseTheme" >
android:label="@string/app_name" >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />

Ways to send out latitude and longitude to android emulator

  1. Open up DDMS point of view in Overshadow (Home window - Open up Point of view)
  2. Choose your emulator device 
  3. Choose the tab called emulator control In ‘Location Controls’ panel, ‘Manual’ tab, provide the Longitude and Latitude as input and ‘Send’.
Disqus Comments