package com.cn.gps;
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 GpsActivity extends Activity {
/** Called when the activity is first created. */
private TextView text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
text = (TextView)findViewById(R.id.tv1);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
showLocation(location);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, new LocationListener(){
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
showLocation(location);
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
showLocation(null);
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
showLocation(locationManager.getLastKnownLocation(provider));
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
});
}
public void showLocation(Location currentLocation){
if(currentLocation != null){
String s = "";
s += "当前位置: (";
s += currentLocation.getLongitude();
s += ",";
s += currentLocation.getLatitude();
s += ")\n 速度: ";
s += currentLocation.getSpeed();
s += "\n 方向: ";
s += currentLocation.getBearing();
s +="\n 高度:";
s += currentLocation.getAltitude();
text.setText(s);
}
else{
text.setText("");
}
}
}