<blockquote >
<p >Android location APIs make it easy for you to build location-aware applications, without needing to focus on the details of the underlying location technology.</p>
</blockquote>
<p >This becomes possible with the help of<b >Google Play services</b>, which facilitates adding location awareness to your app with automated location tracking, geofencing, and activity recognition.</p>
<p >This tutorial shows you how to use Location Services in your APP to get the current location, get periodic location updates, look up addresses etc.</p>
<h2 >The Location Object</h2>
<p >The<b >Location</b>object represents a geographic location which can consist of a latitude, longitude, time stamp, and other information such as bearing, altitude and velocity. There are following important methods which you can use with Location object to get location specific information:</p>
<table class="table table-bordered" >
<tbody >
<tr >
<th >Sr.No.</th>
<th >Method & Description</th>
</tr>
<tr >
<td >1</td>
<td ><b >float distanceTo(Location dest)</b>
<p >Returns the approximate distance in meters between this location and the given location.</p>
</td>
</tr>
<tr >
<td >2</td>
<td ><b >float getAccuracy()</b>
<p >Get the estimated accuracy of this location, in meters.</p>
</td>
</tr>
<tr >
<td >3</td>
<td ><b >double getAltitude()</b>
<p >Get the altitude if available, in meters above sea level.</p>
</td>
</tr>
<tr >
<td >4</td>
<td ><b >float getBearing()</b>
<p >Get the bearing, in degrees.</p>
</td>
</tr>
<tr >
<td >5</td>
<td ><b >double getLatitude()</b>
<p >Get the latitude, in degrees.</p>
</td>
</tr>
<tr >
<td >6</td>
<td ><b >double getLongitude()</b>
<p >Get the longitude, in degrees.</p>
</td>
</tr>
<tr >
<td >7</td>
<td ><b >float getSpeed()</b>
<p >Get the speed if it is available, in meters/second over ground.</p>
</td>
</tr>
<tr >
<td >8</td>
<td ><b >boolean hasAccuracy()</b>
<p >True if this location has an accuracy.</p>
</td>
</tr>
<tr >
<td >9</td>
<td ><b >boolean hasAltitude()</b>
<p >True if this location has an altitude.</p>
</td>
</tr>
<tr >
<td >10</td>
<td ><b >boolean hasBearing()</b>
<p >True if this location has a bearing.</p>
</td>
</tr>
<tr >
<td >11</td>
<td ><b >boolean hasSpeed()</b>
<p >True if this location has a speed.</p>
</td>
</tr>
<tr >
<td >12</td>
<td ><b >void reset()</b>
<p >Clears the contents of the location.</p>
</td>
</tr>
<tr >
<td >13</td>
<td ><b >void setAccuracy(float accuracy)</b>
<p >Set the estimated accuracy of this location, meters.</p>
</td>
</tr>
<tr >
<td >14</td>
<td ><b >void setAltitude(double altitude)</b>
<p >Set the altitude, in meters above sea level.</p>
</td>
</tr>
<tr >
<td >15</td>
<td ><b >void setBearing(float bearing)</b>
<p >Set the bearing, in degrees.</p>
</td>
</tr>
<tr >
<td >16</td>
<td ><b >void setLatitude(double latitude)</b>
<p >Set the latitude, in degrees.</p>
</td>
</tr>
<tr >
<td >17</td>
<td ><b >void setLongitude(double longitude)</b>
<p >Set the longitude, in degrees.</p>
</td>
</tr>
<tr >
<td >18</td>
<td ><b >void setSpeed(float speed)</b>
<p >Set the speed, in meters/second over ground.</p>
</td>
</tr>
<tr >
<td >19</td>
<td ><b >String toString()</b>
<p >Returns a string containing a concise, human-readable description of this object.</p>
</td>
</tr>
</tbody>
</table>
<h2 >Get the Current Location</h2>
<p >To get the current location, create a location client which is<b >LocationClient</b>object, connect it to Location Services using<b >connect()</b>method, and then call its<b >getLastLocation()</b>method. This method returns the most recent location in the form of<b >Location</b>object that contains latitude and longitude coordinates and other information as explained above. To have location based functionality in your activity, you will have to implement two interfaces −</p>
<ul class="list" >
<li >
<p ><b >GooglePlayServicesClient.ConnectionCallbacks</b></p>
</li>
<li >
<p ><b >GooglePlayServicesClient.OnConnectionFailedListener</b></p>
</li>
</ul>
<p >These interfaces provide following important callback methods, which you need to implement in your activity class −</p>
<table class="table table-bordered" >
<tbody >
<tr >
<th >Sr.No.</th>
<th >Callback Methods & Description</th>
</tr>
<tr >
<td >1</td>
<td ><b >abstract void onConnected(Bundle connectionHint)</b>
<p >This callback method is called when location service is connected to the location client successfully. You will use<b >connect()</b>method to connect to the location client.</p>
</td>
</tr>
<tr >
<td >2</td>
<td ><b >abstract void onDisconnected()</b>
<p >This callback method is called when the client is disconnected. You will use<b >disconnect()</b>method to disconnect from the location client.</p>
</td>
</tr>
<tr >
<td >3</td>
<td ><b >abstract void onConnectionFailed(ConnectionResult result)</b>
<p >This callback method is called when there was an error connecting the client to the service.</p>
</td>
</tr>
</tbody>
</table>
<blockquote >You should create the location client in<b >onCreate()</b>method of your activity class, then connect it in<b >onStart()</b>, so that Location Services maintains the current location while your activity is fully visible. You should disconnect the client in<b >onStop()</b>method, so that when your app is not visible, Location Services is not maintaining the current location. This helps in saving battery power up-to a large extent.</blockquote>
<h2 >Get the Updated Location</h2>
<p >If you are willing to have location updates, then apart from above mentioned interfaces, you will need to implement<b >LocationListener</b>interface as well. This interface provide following callback method, which you need to implement in your activity class −</p>
<table class="table table-bordered" >
<tbody >
<tr >
<th >Sr.No.</th>
<th >Callback Method & Description</th>
</tr>
<tr >
<td >1</td>
<td ><b >abstract void onLocationChanged(Location location)</b>
<p >This callback method is used for receiving notifications from the LocationClient when the location has changed.</p>
</td>
</tr>
</tbody>
</table>
<h2 >Location Quality of Service</h2>
<p >The<b >LocationRequest</b>object is used to request a quality of service (QoS) for location updates from the<b >LocationClient</b>. There are following useful setter methods which you can use to handle QoS. There are equivalent getter methods available which you can check in Android official documentation.</p>
<table class="table table-bordered" >
<tbody >
<tr >
<th >Sr.No.</th>
<th >Method & Description</th>
</tr>
<tr >
<td >1</td>
<td ><b >setExpirationDuration(long millis)</b>
<p >Set the duration of this request, in milliseconds.</p>
</td>
</tr>
<tr >
<td >2</td>
<td ><b >setExpirationTime(long millis)</b>
<p >Set the request expiration time, in millisecond since boot.</p>
</td>
</tr>
<tr >
<td >3</td>
<td ><b >setFastestInterval(long millis)</b>
<p >Explicitly set the fastest interval for location updates, in milliseconds.</p>
</td>
</tr>
<tr >
<td >4</td>
<td ><b >setInterval(long millis)</b>
<p >Set the desired interval for active location updates, in milliseconds.</p>
</td>
</tr>
<tr >
<td >5</td>
<td ><b >setNumUpdates(int numUpdates)</b>
<p >Set the number of location updates.</p>
</td>
</tr>
<tr >
<td >6</td>
<td ><b >setPriority(int priority)</b>
<p >Set the priority of the request.</p>
</td>
</tr>
</tbody>
</table>
<p >Now for example, if your application wants high accuracy location it should create a location request with<b >setPriority(int)</b>set to PRIORITY_HIGH_ACCURACY and<b >setInterval(long)</b>to 5 seconds. You can also use bigger interval and/or other priorities like PRIORITY_LOW_POWER for to request "city" level accuracy or PRIORITY_BALANCED_POWER_ACCURACY for "block" level accuracy.</p>
<blockquote >Activities should strongly consider removing all location request when entering the background (for example at onPause()), or at least swap the request to a larger interval and lower quality to save power consumption.</blockquote>
<h2 >Displaying a Location Address</h2>
<p >Once you have<b >Location</b>object, you can use<b >Geocoder.getFromLocation()</b>method to get an address for a given latitude and longitude. This method is synchronous, and may take a long time to do its work, so you should call the method from the<b >doInBackground()</b>method of an<b >AsyncTask</b>class.</p>
<p >The<b >AsyncTask</b>must be subclassed to be used and the subclass will override<b >doInBackground(Params...)</b>method to perform a task in the background and<b >onPostExecute(Result)</b>method is invoked on the UI thread after the background computation finishes and at the time to display the result. There is one more important method available in AyncTask which is<b >execute(Params... params)</b>, this method executes the task with the specified parameters.</p>
<p >Check following example to have better understanding on how we use AynchTask in any Android application to get work done in the background without interfering main task.</p>
<h2 >Example</h2>
<p >Following example shows you in practical how to to use Location Services in your app to get the current location and its equivalent addresses etc.</p>
<blockquote >To experiment with this example, you will need actual Mobile device equipped with latest Android OS, otherwise you will have to struggle with emulator which may not work.</blockquote>
<h3 >Install the Google Play Services SDK</h3>
<p >Before you proceed to have location support in your Android Applications, you need to set-up<b >Google Play Services SDK</b>using following simple steps −</p>
<table class="table table-bordered" >
<tbody >
<tr >
<th class="fivepct" >Steps</th>
<th >Description</th>
</tr>
<tr >
<td >1</td>
<td >Launch Android Studio IDE
<ul class="list" >
<li >
<p >From Android Studio select<b >file >project structure >dependencies > Click on + button to add dependencies</b></p>
</li>
<li >you would get choose library dependencies dialog window</li>
</ul>
</td>
</tr>
<tr >
<td >2</td>
<td >Search for<b >com.google.android.gms:play-services:6.5.87</b>or higher version library. its depend on which android version is using with.</td>
</tr>
<tr >
<td >3</td>
<td >Select the google play services and press apply button as shown below image</td>
</tr>
</tbody>
</table>
<h3 >Create Android Application</h3>
<table class="table table-bordered" >
<tbody >
<tr >
<th class="fivepct" >Step</th>
<th >Description</th>
</tr>
<tr >
<td >1</td>
<td >You will use Android studio IDE to create an Android application and name it as<i >Tutorialspoint</i>under a package<i >com.example.Tutorialspoint</i>. While creating this project, make sure you<i >Target SDK</i>and<i >Compile With</i>at the latest version of Android SDK to use higher levels of APIs.</td>
</tr>
<tr >
<td >2</td>
<td >Add<b >Google Play Service</b>library in your project by following simple steps given below.</td>
</tr>
<tr >
<td >3</td>
<td >Modify<i >src/MainActivity.java</i>file and add required code as shown below to take care of getting current location and its equivalent address.</td>
</tr>
<tr >
<td >4</td>
<td >Modify layout XML file<i >res/layout/activity_main.xml</i>to add all GUI components which include three buttons and two text views to show location/address.</td>
</tr>
<tr >
<td >5</td>
<td >Modify<i >res/values/strings.xml</i>to define required constant values</td>
</tr>
<tr >
<td >6</td>
<td >Modify<i >AndroidManifest.xml</i>as shown below</td>
</tr>
<tr >
<td >7</td>
<td >Run the application to launch Android emulator and verify the result of the changes done in the application.</td>
</tr>
</tbody>
</table>
<p >Let's add<b >Google Play Service</b>reference in the project.Click on file<b >> project structure > dependencies ></b>and select<b >+</b>and then search google play services which will show<i >com.google.android.gms:play-services:6.5.87</i>Click on ok button. it will close the choose dependencies windows. you must be close project structure by clicking apply button</p>
<p><img alt="Google Play Service" src="http://www.tutorialspoint.com/android/images/location1.jpg" /></p>
<p >Above image is showing the result of adding google play services to project. after add google play services to project. It should be as follows</p>
<p><img alt="Location" src="http://www.tutorialspoint.com/android/images/location2.jpg" /></p>
<p >Following is the content of the modified main activity file<b >src/com.example.Tutorialspoint/MainActivity.java</b>.</p>
<pre class="prettyprint notranslate prettyprinted" >
<span class="kwd" >package</span><span class="pln" > com</span><span class="pun" >.</span><span class="pln" >example</span><span class="pun" >.</span><span class="typ" >Tutorialspoint</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > java</span><span class="pun" >.</span><span class="pln" >io</span><span class="pun" >.</span><span class="typ" >IOException</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > java</span><span class="pun" >.</span><span class="pln" >util</span><span class="pun" >.</span><span class="typ" >List</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > java</span><span class="pun" >.</span><span class="pln" >util</span><span class="pun" >.</span><span class="typ" >Locale</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > com</span><span class="pun" >.</span><span class="pln" >google</span><span class="pun" >.</span><span class="pln" >android</span><span class="pun" >.</span><span class="pln" >gms</span><span class="pun" >.</span><span class="pln" >common</span><span class="pun" >.</span><span class="typ" >ConnectionResult</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > com</span><span class="pun" >.</span><span class="pln" >google</span><span class="pun" >.</span><span class="pln" >android</span><span class="pun" >.</span><span class="pln" >gms</span><span class="pun" >.</span><span class="pln" >common</span><span class="pun" >.</span><span class="typ" >GooglePlayServicesClient</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > com</span><span class="pun" >.</span><span class="pln" >google</span><span class="pun" >.</span><span class="pln" >android</span><span class="pun" >.</span><span class="pln" >gms</span><span class="pun" >.</span><span class="pln" >location</span><span class="pun" >.</span><span class="typ" >LocationClient</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >content</span><span class="pun" >.</span><span class="typ" >Context</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >location</span><span class="pun" >.</span><span class="typ" >Address</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >location</span><span class="pun" >.</span><span class="typ" >Geocoder</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >location</span><span class="pun" >.</span><span class="typ" >Location</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >os</span><span class="pun" >.</span><span class="typ" >AsyncTask</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >os</span><span class="pun" >.</span><span class="typ" >Bundle</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >support</span><span class="pun" >.</span><span class="pln" >v4</span><span class="pun" >.</span><span class="pln" >app</span><span class="pun" >.</span><span class="typ" >FragmentActivity</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >util</span><span class="pun" >.</span><span class="typ" >Log</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >view</span><span class="pun" >.</span><span class="typ" >View</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >widget</span><span class="pun" >.</span><span class="typ" >Button</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >widget</span><span class="pun" >.</span><span class="typ" >TextView</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >widget</span><span class="pun" >.</span><span class="typ" >Toast</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >class</span><span class="pln" > </span><span class="typ" >MainActivity</span><span class="pln" > </span><span class="kwd" >extends</span><span class="pln" > </span><span class="typ" >FragmentActivity</span><span class="pln" > </span><span class="kwd" >implements</span><span class="pln" >
</span><span class="typ" >GooglePlayServicesClient</span><span class="pun" >.</span><span class="typ" >ConnectionCallbacks</span><span class="pun" >,</span><span class="pln" >
</span><span class="typ" >GooglePlayServicesClient</span><span class="pun" >.</span><span class="typ" >OnConnectionFailedListener</span><span class="pln" >
</span><span class="pun" >{</span><span class="pln" >
</span><span class="typ" >LocationClient</span><span class="pln" > mLocationClient</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >private</span><span class="pln" > </span><span class="typ" >TextView</span><span class="pln" > addressLabel</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >private</span><span class="pln" > </span><span class="typ" >TextView</span><span class="pln" > locationLabel</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >private</span><span class="pln" > </span><span class="typ" >Button</span><span class="pln" > getLocationBtn</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >private</span><span class="pln" > </span><span class="typ" >Button</span><span class="pln" > disconnectBtn</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >private</span><span class="pln" > </span><span class="typ" >Button</span><span class="pln" > connectBtn</span><span class="pun" >;</span><span class="pln" >
</span><span class="lit" >@Override</span><span class="pln" >
</span><span class="kwd" >protected</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > onCreate</span><span class="pun" >(</span><span class="typ" >Bundle</span><span class="pln" > savedInstanceState</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="kwd" >super</span><span class="pun" >.</span><span class="pln" >onCreate</span><span class="pun" >(</span><span class="pln" >savedInstanceState</span><span class="pun" >);</span><span class="pln" >
setContentView</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >layout</span><span class="pun" >.</span><span class="pln" >activity_main</span><span class="pun" >);</span><span class="pln" >
locationLabel </span><span class="pun" >=</span><span class="pln" > </span><span class="pun" >(</span><span class="typ" >TextView</span><span class="pun" >)</span><span class="pln" > findViewById</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >id</span><span class="pun" >.</span><span class="pln" >locationLabel</span><span class="pun" >);</span><span class="pln" >
addressLabel </span><span class="pun" >=</span><span class="pln" > </span><span class="pun" >(</span><span class="typ" >TextView</span><span class="pun" >)</span><span class="pln" > findViewById</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >id</span><span class="pun" >.</span><span class="pln" >addressLabel</span><span class="pun" >);</span><span class="pln" >
getLocationBtn </span><span class="pun" >=</span><span class="pln" > </span><span class="pun" >(</span><span class="typ" >Button</span><span class="pun" >)</span><span class="pln" > findViewById</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >id</span><span class="pun" >.</span><span class="pln" >getLocation</span><span class="pun" >);</span><span class="pln" >
getLocationBtn</span><span class="pun" >.</span><span class="pln" >setOnClickListener</span><span class="pun" >(</span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >View</span><span class="pun" >.</span><span class="typ" >OnClickListener</span><span class="pun" >()</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > onClick</span><span class="pun" >(</span><span class="typ" >View</span><span class="pln" > view</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
displayCurrentLocation</span><span class="pun" >();</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="pun" >});</span><span class="pln" >
disconnectBtn </span><span class="pun" >=</span><span class="pln" > </span><span class="pun" >(</span><span class="typ" >Button</span><span class="pun" >)</span><span class="pln" > findViewById</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >id</span><span class="pun" >.</span><span class="pln" >disconnect</span><span class="pun" >);</span><span class="pln" >
disconnectBtn</span><span class="pun" >.</span><span class="pln" >setOnClickListener</span><span class="pun" >(</span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >View</span><span class="pun" >.</span><span class="typ" >OnClickListener</span><span class="pun" >()</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > onClick</span><span class="pun" >(</span><span class="typ" >View</span><span class="pln" > view</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
mLocationClient</span><span class="pun" >.</span><span class="pln" >disconnect</span><span class="pun" >();</span><span class="pln" >
locationLabel</span><span class="pun" >.</span><span class="pln" >setText</span><span class="pun" >(</span><span class="str" >"Got disconnected...."</span><span class="pun" >);</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="pun" >});</span><span class="pln" >
connectBtn </span><span class="pun" >=</span><span class="pln" > </span><span class="pun" >(</span><span class="typ" >Button</span><span class="pun" >)</span><span class="pln" > findViewById</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >id</span><span class="pun" >.</span><span class="pln" >connect</span><span class="pun" >);</span><span class="pln" >
connectBtn</span><span class="pun" >.</span><span class="pln" >setOnClickListener</span><span class="pun" >(</span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >View</span><span class="pun" >.</span><span class="typ" >OnClickListener</span><span class="pun" >()</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > onClick</span><span class="pun" >(</span><span class="typ" >View</span><span class="pln" > view</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
mLocationClient</span><span class="pun" >.</span><span class="pln" >connect</span><span class="pun" >();</span><span class="pln" >
locationLabel</span><span class="pun" >.</span><span class="pln" >setText</span><span class="pun" >(</span><span class="str" >"Got connected...."</span><span class="pun" >);</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="pun" >});</span><span class="pln" >
</span><span class="com" >// Create the LocationRequest object</span><span class="pln" >
mLocationClient </span><span class="pun" >=</span><span class="pln" > </span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >LocationClient</span><span class="pun" >(</span><span class="kwd" >this</span><span class="pun" >,</span><span class="pln" > </span><span class="kwd" >this</span><span class="pun" >,</span><span class="pln" > </span><span class="kwd" >this</span><span class="pun" >);</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="lit" >@Override</span><span class="pln" >
</span><span class="kwd" >protected</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > onStart</span><span class="pun" >()</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="kwd" >super</span><span class="pun" >.</span><span class="pln" >onStart</span><span class="pun" >();</span><span class="pln" >
</span><span class="com" >// Connect the client.</span><span class="pln" >
mLocationClient</span><span class="pun" >.</span><span class="pln" >connect</span><span class="pun" >();</span><span class="pln" >
locationLabel</span><span class="pun" >.</span><span class="pln" >setText</span><span class="pun" >(</span><span class="str" >"Got connected...."</span><span class="pun" >);</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="lit" >@Override</span><span class="pln" >
</span><span class="kwd" >protected</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > onStop</span><span class="pun" >()</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="com" >// Disconnect the client.</span><span class="pln" >
mLocationClient</span><span class="pun" >.</span><span class="pln" >disconnect</span><span class="pun" >();</span><span class="pln" >
</span><span class="kwd" >super</span><span class="pun" >.</span><span class="pln" >onStop</span><span class="pun" >();</span><span class="pln" >
locationLabel</span><span class="pun" >.</span><span class="pln" >setText</span><span class="pun" >(</span><span class="str" >"Got disconnected...."</span><span class="pun" >);</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="lit" >@Override</span><span class="pln" >
</span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > onConnected</span><span class="pun" >(</span><span class="typ" >Bundle</span><span class="pln" > dataBundle</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="com" >// Display the connection status</span><span class="pln" >
</span><span class="typ" >Toast</span><span class="pun" >.</span><span class="pln" >makeText</span><span class="pun" >(</span><span class="kwd" >this</span><span class="pun" >,</span><span class="pln" > </span><span class="str" >"Connected"</span><span class="pun" >,</span><span class="pln" > </span><span class="typ" >Toast</span><span class="pun" >.</span><span class="pln" >LENGTH_SHORT</span><span class="pun" >).</span><span class="pln" >show</span><span class="pun" >();</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="lit" >@Override</span><span class="pln" >
</span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > onDisconnected</span><span class="pun" >()</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="com" >// Display the connection status</span><span class="pln" >
</span><span class="typ" >Toast</span><span class="pun" >.</span><span class="pln" >makeText</span><span class="pun" >(</span><span class="kwd" >this</span><span class="pun" >,</span><span class="pln" > </span><span class="str" >"Disconnected. Please re-connect."</span><span class="pun" >,</span><span class="typ" >Toast</span><span class="pun" >.</span><span class="pln" >LENGTH_SHORT</span><span class="pun" >).</span><span class="pln" >show</span><span class="pun" >();</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="lit" >@Override</span><span class="pln" >
</span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > onConnectionFailed</span><span class="pun" >(</span><span class="typ" >ConnectionResult</span><span class="pln" > connectionResult</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="com" >// Display the error code on failure</span><span class="pln" >
</span><span class="typ" >Toast</span><span class="pun" >.</span><span class="pln" >makeText</span><span class="pun" >(</span><span class="kwd" >this</span><span class="pun" >,</span><span class="pln" > </span><span class="str" >"Connection Failure : "</span><span class="pln" > </span><span class="pun" >+</span><span class="pln" > connectionResult</span><span class="pun" >.</span><span class="pln" >getErrorCode</span><span class="pun" >(),</span><span class="typ" >Toast</span><span class="pun" >.</span><span class="pln" >LENGTH_SHORT</span><span class="pun" >).</span><span class="pln" >show</span><span class="pun" >();</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > displayCurrentLocation</span><span class="pun" >()</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="com" >// Get the current location's latitude & longitude</span><span class="pln" >
</span><span class="typ" >Location</span><span class="pln" > currentLocation </span><span class="pun" >=</span><span class="pln" > mLocationClient</span><span class="pun" >.</span><span class="pln" >getLastLocation</span><span class="pun" >();</span><span class="pln" >
</span><span class="typ" >String</span><span class="pln" > msg </span><span class="pun" >=</span><span class="pln" > </span><span class="str" >"Current Location: "</span><span class="pln" > </span><span class="pun" >+</span><span class="pln" >
</span><span class="typ" >Double</span><span class="pun" >.</span><span class="pln" >toString</span><span class="pun" >(</span><span class="pln" >currentLocation</span><span class="pun" >.</span><span class="pln" >getLatitude</span><span class="pun" >())</span><span class="pln" > </span><span class="pun" >+</span><span class="pln" > </span><span class="str" >","</span><span class="pln" > </span><span class="pun" >+</span><span class="pln" >
</span><span class="typ" >Double</span><span class="pun" >.</span><span class="pln" >toString</span><span class="pun" >(</span><span class="pln" >currentLocation</span><span class="pun" >.</span><span class="pln" >getLongitude</span><span class="pun" >());</span><span class="pln" >
</span><span class="com" >// Display the current location in the UI</span><span class="pln" >
locationLabel</span><span class="pun" >.</span><span class="pln" >setText</span><span class="pun" >(</span><span class="pln" >msg</span><span class="pun" >);</span><span class="pln" >
</span><span class="com" >// To display the current address in the UI</span><span class="pln" >
</span><span class="pun" >(</span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >GetAddressTask</span><span class="pun" >(</span><span class="kwd" >this</span><span class="pun" >)).</span><span class="pln" >execute</span><span class="pun" >(</span><span class="pln" >currentLocation</span><span class="pun" >);</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="com" >/*
* Following is a subclass of AsyncTask which has been used to get
* address corresponding to the given latitude & longitude.
*/</span><span class="pln" >
</span><span class="kwd" >private</span><span class="pln" > </span><span class="kwd" >class</span><span class="pln" > </span><span class="typ" >GetAddressTask</span><span class="pln" > </span><span class="kwd" >extends</span><span class="pln" > </span><span class="typ" >AsyncTask</span><span class="pun" ><</span><span class="typ" >Location</span><span class="pun" >,</span><span class="pln" > </span><span class="typ" >Void</span><span class="pun" >,</span><span class="pln" > </span><span class="typ" >String</span><span class="pun" >>{</span><span class="pln" >
</span><span class="typ" >Context</span><span class="pln" > mContext</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >public</span><span class="pln" > </span><span class="typ" >GetAddressTask</span><span class="pun" >(</span><span class="typ" >Context</span><span class="pln" > context</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="kwd" >super</span><span class="pun" >();</span><span class="pln" >
mContext </span><span class="pun" >=</span><span class="pln" > context</span><span class="pun" >;</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="com" >/*
* When the task finishes, onPostExecute() displays the address.
*/</span><span class="pln" >
</span><span class="lit" >@Override</span><span class="pln" >
</span><span class="kwd" >protected</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > onPostExecute</span><span class="pun" >(</span><span class="typ" >String</span><span class="pln" > address</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="com" >// Display the current address in the UI</span><span class="pln" >
addressLabel</span><span class="pun" >.</span><span class="pln" >setText</span><span class="pun" >(</span><span class="pln" >address</span><span class="pun" >);</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="lit" >@Override</span><span class="pln" >
</span><span class="kwd" >protected</span><span class="pln" > </span><span class="typ" >String</span><span class="pln" > doInBackground</span><span class="pun" >(</span><span class="typ" >Location</span><span class="pun" >...</span><span class="pln" > </span><span class="kwd" >params</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="typ" >Geocoder</span><span class="pln" > geocoder </span><span class="pun" >=</span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >Geocoder</span><span class="pun" >(</span><span class="pln" >mContext</span><span class="pun" >,</span><span class="pln" > </span><span class="typ" >Locale</span><span class="pun" >.</span><span class="pln" >getDefault</span><span class="pun" >());</span><span class="pln" >
</span><span class="com" >// Get the current location from the input parameter list</span><span class="pln" >
</span><span class="typ" >Location</span><span class="pln" > loc </span><span class="pun" >=</span><span class="pln" > </span><span class="kwd" >params</span><span class="pun" >[</span><span class="lit" >0</span><span class="pun" >];</span><span class="pln" >
</span><span class="com" >// Create a list to contain the result address</span><span class="pln" >
</span><span class="pun" ><</span><span class="typ" >Address</span><span class="pun" >></span><span class="pln" > addresses </span><span class="pun" >=</span><span class="pln" > </span><span class="kwd" >null</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >try</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
addresses </span><span class="pun" >=</span><span class="pln" > geocoder</span><span class="pun" >.</span><span class="pln" >getFromLocation</span><span class="pun" >(</span><span class="pln" >loc</span><span class="pun" >.</span><span class="pln" >getLatitude</span><span class="pun" >(),</span><span class="pln" >loc</span><span class="pun" >.</span><span class="pln" >getLongitude</span><span class="pun" >(),</span><span class="pln" > </span><span class="lit" >1</span><span class="pun" >);</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="kwd" >catch</span><span class="pln" > </span><span class="pun" >(</span><span class="typ" >IOException</span><span class="pln" > e1</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="typ" >Log</span><span class="pun" >.</span><span class="pln" >e</span><span class="pun" >(</span><span class="str" >"LocationSampleActivity"</span><span class="pun" >,</span><span class="pln" >IO </span><span class="typ" >Exception</span><span class="pln" > </span><span class="kwd" >in</span><span class="pln" > getFromLocation</span><span class="pun" >());</span><span class="pln" >
e1</span><span class="pun" >.</span><span class="pln" >printStackTrace</span><span class="pun" >();</span><span class="pln" >
</span><span class="kwd" >return</span><span class="pln" > </span><span class="pun" >(</span><span class="str" >"IO Exception trying to get address"</span><span class="pun" >);</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="kwd" >catch</span><span class="pln" > </span><span class="pun" >(</span><span class="typ" >IllegalArgumentException</span><span class="pln" > e2</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="com" >// Error message to post in the log</span><span class="pln" >
</span><span class="typ" >String</span><span class="pln" > errorString </span><span class="pun" >=</span><span class="pln" > </span><span class="str" >"Illegal arguments "</span><span class="pln" > </span><span class="pun" >+</span><span class="pln" >
</span><span class="typ" >Double</span><span class="pun" >.</span><span class="pln" >toString</span><span class="pun" >(</span><span class="pln" >loc</span><span class="pun" >.</span><span class="pln" >getLatitude</span><span class="pun" >())</span><span class="pln" > </span><span class="pun" >+</span><span class="str" >" , "</span><span class="pln" > </span><span class="pun" >+</span><span class="typ" >Double</span><span class="pun" >.</span><span class="pln" >toString</span><span class="pun" >(</span><span class="pln" >loc</span><span class="pun" >.</span><span class="pln" >getLongitude</span><span class="pun" >())</span><span class="pln" > </span><span class="pun" >+</span><span class="str" >" passed to address service"</span><span class="pun" >;</span><span class="pln" >
</span><span class="typ" >Log</span><span class="pun" >.</span><span class="pln" >e</span><span class="pun" >(</span><span class="str" >"LocationSampleActivity"</span><span class="pun" >,</span><span class="pln" > errorString</span><span class="pun" >);</span><span class="pln" >
e2</span><span class="pun" >.</span><span class="pln" >printStackTrace</span><span class="pun" >();</span><span class="pln" >
</span><span class="kwd" >return</span><span class="pln" > errorString</span><span class="pun" >;</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="com" >// If the reverse geocode returned an address</span><span class="pln" >
</span><span class="kwd" >if</span><span class="pln" > </span><span class="pun" >(</span><span class="pln" >addresses </span><span class="pun" >!=</span><span class="pln" > </span><span class="kwd" >null</span><span class="pln" > </span><span class="pun" >&&</span><span class="pln" > addresses</span><span class="pun" >.</span><span class="pln" >size</span><span class="pun" >()</span><span class="pln" > </span><span class="pun" >></span><span class="pln" > </span><span class="lit" >0</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="com" >// Get the first address</span><span class="pln" >
</span><span class="typ" >Address</span><span class="pln" > address </span><span class="pun" >=</span><span class="pln" > addresses</span><span class="pun" >.</span><span class="kwd" >get</span><span class="pun" >(</span><span class="lit" >0</span><span class="pun" >);</span><span class="pln" >
</span><span class="com" >/*
* Format the first line of address (if available),
* city, and country name.
*/</span><span class="pln" >
</span><span class="typ" >String</span><span class="pln" > addressText </span><span class="pun" >=</span><span class="pln" > </span><span class="typ" >String</span><span class="pun" >.</span><span class="pln" >format</span><span class="pun" >(</span><span class="str" >"%s, %s, %s"</span><span class="pun" >);</span><span class="pln" >
</span><span class="com" >// If there's a street address, add it</span><span class="pln" >
address</span><span class="pun" >.</span><span class="pln" >getMaxAddressLineIndex</span><span class="pun" >()</span><span class="pln" > </span><span class="pun" >></span><span class="pln" > </span><span class="lit" >0</span><span class="pln" > </span><span class="pun" >?</span><span class="pln" >
address</span><span class="pun" >.</span><span class="pln" >getAddressLine</span><span class="pun" >(</span><span class="lit" >0</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >:</span><span class="pln" > </span><span class="str" >""</span><span class="pun" >,</span><span class="pln" >
</span><span class="com" >// Locality is usually a city</span><span class="pln" >
address</span><span class="pun" >.</span><span class="pln" >getLocality</span><span class="pun" >(),</span><span class="pln" >
</span><span class="com" >// The country of the address</span><span class="pln" >
address</span><span class="pun" >.</span><span class="pln" >getCountryName</span><span class="pun" >());</span><span class="pln" >
</span><span class="com" >// Return the text</span><span class="pln" >
</span><span class="kwd" >return</span><span class="pln" > addressText</span><span class="pun" >;</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="kwd" >else</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="kwd" >return</span><span class="pln" > </span><span class="str" >"No address found"</span><span class="pun" >;</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="pun" >}</span><span class="com" >// AsyncTask class</span><span class="pln" >
</span><span class="pun" >}</span></pre>
<p >Following will be the content of<b >res/layout/activity_main.xml</b>file −</p>
<pre class="prettyprint notranslate prettyprinted" >
<span class="tag" ><LinearLayout</span><span class="pln" > </span><span class="atn" >xmlns:android</span><span class="pun" >=</span><span class="atv" >"http://schemas.android.com/apk/res/android"</span><span class="pln" >
</span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >"fill_parent"</span><span class="pln" >
</span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >"fill_parent"</span><span class="pln" >
</span><span class="atn" >android:orientation</span><span class="pun" >=</span><span class="atv" >"vertical"</span><span class="pln" > </span><span class="tag" >></span><span class="pln" >
</span><span class="tag" ><TextView</span><span class="pln" > </span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >"@+id/textView1"</span><span class="pln" >
</span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:text</span><span class="pun" >=</span><span class="atv" >"Location Example"</span><span class="pln" >
</span><span class="atn" >android:layout_alignParentTop</span><span class="pun" >=</span><span class="atv" >"true"</span><span class="pln" >
</span><span class="atn" >android:layout_centerHorizontal</span><span class="pun" >=</span><span class="atv" >"true"</span><span class="pln" >
</span><span class="atn" >android:textSize</span><span class="pun" >=</span><span class="atv" >"30dp"</span><span class="pln" > </span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ><TextView</span><span class="pln" > </span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >"@+id/textView2"</span><span class="pln" >
</span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:text</span><span class="pun" >=</span><span class="atv" >"Tutorials point "</span><span class="pln" >
</span><span class="atn" >android:textColor</span><span class="pun" >=</span><span class="atv" >"#ff87ff09"</span><span class="pln" >
</span><span class="atn" >android:textSize</span><span class="pun" >=</span><span class="atv" >"30dp"</span><span class="pln" >
</span><span class="atn" >android:layout_below</span><span class="pun" >=</span><span class="atv" >"@+id/textView1"</span><span class="pln" >
</span><span class="atn" >android:layout_alignRight</span><span class="pun" >=</span><span class="atv" >"@+id/imageButton"</span><span class="pln" >
</span><span class="atn" >android:layout_alignEnd</span><span class="pun" >=</span><span class="atv" >"@+id/imageButton"</span><span class="pln" > </span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ><ImageButton</span><span class="pln" > </span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >"@+id/imageButton"</span><span class="pln" >
</span><span class="atn" >android:src</span><span class="pun" >=</span><span class="atv" >"@drawable/abc"</span><span class="pln" >
</span><span class="atn" >android:layout_below</span><span class="pun" >=</span><span class="atv" >"@+id/textView2"</span><span class="pln" >
</span><span class="atn" >android:layout_centerHorizontal</span><span class="pun" >=</span><span class="atv" >"true"</span><span class="pln" > </span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ><Button</span><span class="pln" > </span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >"@+id/getLocation"</span><span class="pln" >
</span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >"fill_parent"</span><span class="pln" >
</span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:text</span><span class="pun" >=</span><span class="atv" >"@string/get_location"</span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ><Button</span><span class="pln" > </span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >"@+id/disconnect"</span><span class="pln" >
</span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >"fill_parent"</span><span class="pln" >
</span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:text</span><span class="pun" >=</span><span class="atv" >"@string/disconnect"</span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ><Button</span><span class="pln" > </span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >"@+id/connect"</span><span class="pln" >
</span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >"fill_parent"</span><span class="pln" >
</span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:text</span><span class="pun" >=</span><span class="atv" >"@string/connect"</span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ><TextView</span><span class="pln" >
</span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >"@+id/locationLabel"</span><span class="pln" >
</span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ><TextView</span><span class="pln" >
</span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >"@+id/addressLabel"</span><span class="pln" >
</span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ></LinearLayout></span></pre>
<p >Following will be the content of<b >res/values/strings.xml</b>to define two new constants:</p>
<pre class="prettyprint notranslate prettyprinted" >
<span class="pun" ><?</span><span class="pln" >xml version</span><span class="pun" >=</span><span class="str" >"1.0"</span><span class="pln" > encoding</span><span class="pun" >=</span><span class="str" >"utf-8"</span><span class="pun" >?></span><span class="pln" >
</span><span class="tag" ><resources></span><span class="pln" >
</span><span class="tag" ><string</span><span class="pln" > </span><span class="atn" >name</span><span class="pun" >=</span><span class="atv" >"app_name"</span><span class="tag" >></span><span class="pln" >Tutorialspoint</span><span class="tag" ></string></span><span class="pln" >
</span><span class="tag" ><string</span><span class="pln" > </span><span class="atn" >name</span><span class="pun" >=</span><span class="atv" >"action_settings"</span><span class="tag" >></span><span class="pln" >Settings</span><span class="tag" ></string></span><span class="pln" >
</span><span class="tag" ><string</span><span class="pln" > </span><span class="atn" >name</span><span class="pun" >=</span><span class="atv" >"hello_world"</span><span class="tag" >></span><span class="pln" >Hello world!</span><span class="tag" ></string></span><span class="pln" >
</span><span class="tag" ><string</span><span class="pln" > </span><span class="atn" >name</span><span class="pun" >=</span><span class="atv" >"get_location"</span><span class="tag" >></span><span class="pln" >Get Location</span><span class="tag" ></string></span><span class="pln" >
</span><span class="tag" ><string</span><span class="pln" > </span><span class="atn" >name</span><span class="pun" >=</span><span class="atv" >"disconnect"</span><span class="tag" >></span><span class="pln" >Disconnect Service</span><span class="tag" ></string></span><span class="pln" >
</span><span class="tag" ><string</span><span class="pln" > </span><span class="atn" >name</span><span class="pun" >=</span><span class="atv" >"connect"</span><span class="tag" >></span><span class="pln" >Connect Service</span><span class="tag" ></string></span><span class="pln" >
</span><span class="tag" ></resources></span></pre>
<p >Following is the default content of<b >AndroidManifest.xml</b>−</p>
<pre class="prettyprint notranslate prettyprinted" >
<span class="pun" ><?</span><span class="pln" >xml version</span><span class="pun" >=</span><span class="str" >"1.0"</span><span class="pln" > encoding</span><span class="pun" >=</span><span class="str" >"utf-8"</span><span class="pun" >?></span><span class="pln" >
</span><span class="tag" ><manifest</span><span class="pln" > </span><span class="atn" >xmlns:android</span><span class="pun" >=</span><span class="atv" >"http://schemas.android.com/apk/res/android"</span><span class="pln" >
</span><span class="atn" >package</span><span class="pun" >=</span><span class="atv" >"com.example.Tutorialspoint"</span><span class="pln" >
</span><span class="atn" >android:versionCode</span><span class="pun" >=</span><span class="atv" >"1"</span><span class="pln" >
</span><span class="atn" >android:versionName</span><span class="pun" >=</span><span class="atv" >"1.0"</span><span class="pln" > </span><span class="tag" >></span><span class="pln" >
</span><span class="tag" ><uses-permission</span><span class="pln" > </span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >"android.permission.ACCESS_COARSE_LOCATION"</span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ><application</span><span class="pln" >
</span><span class="atn" >android:allowBackup</span><span class="pun" >=</span><span class="atv" >"true"</span><span class="pln" >
</span><span class="atn" >android:icon</span><span class="pun" >=</span><span class="atv" >"@drawable/ic_launcher"</span><span class="pln" >
</span><span class="atn" >android:label</span><span class="pun" >=</span><span class="atv" >"@string/app_name"</span><span class="pln" >
</span><span class="atn" >android:theme</span><span class="pun" >=</span><span class="atv" >"@style/AppTheme"</span><span class="pln" > </span><span class="tag" >></span><span class="pln" >
</span><span class="tag" ><activity</span><span class="pln" >
</span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >"com.example.Tutorialspoint.MainActivity"</span><span class="pln" >
</span><span class="atn" >android:label</span><span class="pun" >=</span><span class="atv" >"@string/app_name"</span><span class="pln" > </span><span class="tag" >></span><span class="pln" >
</span><span class="tag" ><intent-filter></span><span class="pln" >
</span><span class="tag" ><action</span><span class="pln" > </span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >"android.intent.action.MAIN"</span><span class="pln" > </span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ><category</span><span class="pln" > </span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >"android.intent.category.LAUNCHER"</span><span class="pln" > </span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ></intent-filter></span><span class="pln" >
</span><span class="tag" ></activity></span><span class="pln" >
</span><span class="tag" ></application></span><span class="pln" >
</span><span class="tag" ></manifest></span></pre>
<p >Let's try to run your<b >Tutorialspoint</b>application. I assume that, you have connected your actual Android Mobile device with your computer. To run the app from Android Studio, open one of your project's activity files and click Run<img alt="Eclipse Run Icon" class="inline" src="http://www.tutorialspoint.com/android/images/eclipse_run.jpg" />icon from the toolbar. Before starting your application, Android studio installer will display following window to select an option where you want to run your Android application.</p>
<p><img alt="Android Mobile Device" src="http://www.tutorialspoint.com/android/images/location3.jpg" /></p>
<p >Now to see location select Get Location Button which will display location information as follows −</p>
<p><img alt="Android Mobile Location Info" src="http://www.tutorialspoint.com/android/images/location4.jpg" /></p>
<p >You can try by disconnecting location client using<b >Disconnect Service</b>and then connecting it by using<b >Connect Service</b>button. You can also modify to get location update as explained above and in Android Official documentation.</p>