Using GPS features in your Android Application
Little Bit of Background to run GPS example in android device.
1. To add GPS Functionality in your Android application, you will have to add the ACCESS_FINE_LOCATION permission to the androidManifest.xml
e.g.
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android” package=”net.learn2develop.GPS”> <uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION” />
<application android:icon=”@drawable/icon” android:label=”@string/app_name”>
<activity android:name=”.GPS” 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>
2. In Android, Loc based services are provided by the
– LocationManager class in android.location package.
Your device can obtain periodic updates of the device’s geographical locations. Android application fires intent when it enters the proximity of a certain location.
3. Connect to android console to give commands to androids host (e.g. Emulator or device)…
tools>telnet
telnet> 0 localhost 5554
(this will connect to the currently active device….)
geo fix -77.230488 38.916336
4. Sample Code … Thread locThread = new Thread(new Runnable(){
public void run() {
LocationListenerImpl loclistImpl= new LocationListenerImpl (this);LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, loclistImpl);
Location loc = this.getLocation();
// Get Location With these two methods…
// loc.getLatitude();
// loc.getLongitude();
}});
locThread.start();
/*LocationListenerImpl class that listenes for Geo location…*/
public class LocationListenerImpl implements LocationListener{
LocationCallBack locationCallBack;
public LocationListenerImpl (LocationCallBack locationCallBack) {
super();this.locationCallBack = locationCallBack;
}
public void onLocationChanged(Location loc) {
locationCallBack.setLocation(loc);
}
public void onProviderDisabled(String arg0) {}
public void onProviderEnabled(String arg0) {}
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {}
}
***After running the code , You will have to run commands given in point 3. So that LocationListenerImpl’s onLocationChanged method will get a call with the provided location ***

Leave a Reply