Read GPS location, LocationManager

It's a activity implement LocationListener. When location changed, the method onLocationChanged() will be called-back.


Create a activity AndroidLocation


To let your application to access GPS function, ACCESS_FINE_LOCATION permission is needed. Add the code
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
to AndroidManifest.xml.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.AndroidLocation"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidLocation"
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-sdk android:minSdkVersion="5" />
</manifest>


Modify main.xml to have 2 TextView to show location from GPS.
<?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"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Latitude:"
/>
<TextView
android:id="@+id/Latitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Longitude:"
/>
<TextView
android:id="@+id/Longitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


Modify the source Code, AndroidLocation.java:
package com.AndroidLocation;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;

public class AndroidLocation extends Activity {

private LocationManager myLocationManager;
private LocationListener myLocationListener;
private TextView myLatitude, myLongitude;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myLatitude = (TextView)findViewById(R.id.Latitude);
myLongitude = (TextView)findViewById(R.id.Longitude);

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

myLocationListener = new MyLocationListener();

myLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0,
myLocationListener);

//Get the current location in start-up
myLatitude.setText(String.valueOf(
myLocationManager.getLastKnownLocation(
LocationManager.GPS_PROVIDER).getLatitude()));

myLongitude.setText(String.valueOf(
myLocationManager.getLastKnownLocation(
LocationManager.GPS_PROVIDER).getLongitude()));

}

private class MyLocationListener implements LocationListener{

public void onLocationChanged(Location argLocation) {
// TODO Auto-generated method stub
myLatitude.setText(String.valueOf(
argLocation.getLatitude()));
myLongitude.setText(String.valueOf(
argLocation.getLongitude()));
}

public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}

public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}

public void onStatusChanged(String provider,
int status, Bundle extras) {
// TODO Auto-generated method stub
}
};
}


Download the files.

May be you have to Enable "Use wireless networks" and "Use GPS satellites" in Android Emulator.

And also Change GPS location of Android Emulator, to test the function of the Application.





Because te application read location in start-up,
so you have to send GPS Location to Android Emulator
at least once before the application start.

Edited@11-11-2009