Slideshow

Tuesday, December 13, 2011

Android GPS Sample Application

Read Conceptual part from developer.android.com

===========================================================
main.xml
===========================================================
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello"

android:id="@+id/tv"

/>

</LinearLayout>
=======================================================
AndroidManifest.XML
=======================================================
<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="vegam.gps_demo"

android:versionCode="1"

android:versionName="1.0">

<application android:icon="@drawable/icon" android:label="@string/app_name">

<activity android:name=".gps_demo"

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>

<uses-permission

android:name="android.permission.ACCESS_FINE_LOCATION" />

<uses-permission

android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />

<uses-permission

android:name="android.permission.CONTROL_LOCATION_UPDATES" />

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

</manifest>
=======================================================
Code Part
=======================================================
package vegam.gps_demo;

import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.util.Log;
import android.widget.TextView;
public class gps_demo extends Activity implements LocationListener {


private LocationManager lm;

private TextView tv;
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.tv);

lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 1, this);

Location l1=new Location(LocationManager.GPS_PROVIDER);

String lat = String.valueOf(l1.getLatitude());

String lon = String.valueOf(l1.getLongitude());

Log.e("GPS", "location changed: lat="+lat+", lon="+lon);

tv.setText("lat="+lat+", lon="+lon);
}

public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
String lat = String.valueOf(location.getLatitude());

String lon = String.valueOf(location.getLongitude());

Log.e("GPS", "location changed: lat="+lat+", lon="+lon);

tv.setText("lat="+lat+", lon="+lon);

}

public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Log.e("GPS", "provider disabled " + provider);

}

public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Log.e("GPS", "provider enabled " + provider);

}

public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
Log.e("GPS", "status changed to " + provider + " [" + status + "]");

}
}
=================================================================
What to do to activate GPS on avd

1. do telnet to emulator by typing : telnet localhost 5554
where 5554 is the name of avd running

2.with the telnet window set/change the geo code by typing

geo fix 72.74 45.6
the changes will appear on avd

If you are using windowsXP for development then you can inspire yourself with the image below;

0 comments:

Post a Comment