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.
Network Place Service provider vs GPS Place Provider
- Network Place service provider is relatively quick compared to the GPS service provider in offering the plane coordinates.
- GPS service provider might be really very slow down in in-door places and will drainpipe the mobile battery.
- Network place service provider depends upon the cell loom and will return our closest loom place.
- A GPS place service provider will provide our place precisely.
Actions to obtain the place in Android
- Offer consents in show submit for getting place update
- Produce LocationManager circumstances as a recommendation to the place service
- Ask for a place from LocationManager
- 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" /></manifest>
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,
package com.javapapers.android.geolocationfinder;import android.os.Bundle;import android.app.Activity;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;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);txtLat = (TextView) findViewById(R.id.textview1);locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);}@Overridepublic void onLocationChanged(Location location) {txtLat = (TextView) findViewById(R.id.textview1);txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude());}@Overridepublic void onProviderDisabled(String provider) {Log.d("Latitude","disable");}@Overridepublic void onProviderEnabled(String provider) {Log.d("Latitude","enable");}@Overridepublic void onStatusChanged(String provider, int status, Bundle extras) {Log.d("Latitude","status");}}
XML files for layout and android manifest are as shown below
<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"tools:context=".MainActivity" ><TextViewandroid:id="@+id/textview1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:layout_centerVertical="true"android:text="@string/hello_world" /></RelativeLayout>
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.javapapers.android.geolocationfinder"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="17" /><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppBaseTheme" ><activityandroid:name="com.javapapers.android.geolocationfinder.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>
Ways to send out latitude and longitude to android emulator
- Open up DDMS point of view in Overshadow (Home window - Open up Point of view)
- Choose your emulator device
- Choose the tab called emulator control In ‘Location Controls’ panel, ‘Manual’ tab, provide the Longitude and Latitude as input and ‘Send’.