package org.jpn.techbooster.sample.location;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.location.Location;
import android.location.LocationManager;
import android.location.LocationListener;
import android.location.LocationProvider;
import android.util.Log;
import android.widget.TextView;
public class LocationActivity extends Activity implements LocationListener {
private LocationManager locationManager_;
private LocationProvider locationProvider_;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public void onStart() {
super.onStart();
TextView providerLabel = (TextView)findViewById(R.id.ProviderLabel);
//
locationManager_ = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
// GPS
locationProvider_ = locationManager_.getProvider(LocationManager.GPS_PROVIDER);
if (null == locationProvider_) {
// GPS
locationProvider_ = locationManager_.getProvider(LocationManager.NETWORK_PROVIDER);
if (null == locationProvider_) {
return;
} else {
providerLabel.setText("NETWORK_PROVIDER");
}
} else {
providerLabel.setText("GPS_PROVIDER");
}
//
locationManager_.requestLocationUpdates(locationProvider_.getName(),
0,
0,
this);
}
@Override
public void onStop() {
super.onStop();
//
locationManager_.removeUpdates(this);
}
@Override
public void onLocationChanged(Location location) {
// TextView
TextView latitudeLabel = (TextView)findViewById(R.id.LatitudeLabel);
TextView LongitudeLabel = (TextView)findViewById(R.id.LongitudeLabel);
latitudeLabel.setText(Double.toString(location.getLatitude()));
LongitudeLabel.setText(Double.toString(location.getLongitude()));
}
@Override
public void onProviderEnabled(String provider) {
Log.d("LocationActivity","onProviderEnabled");
}
@Override
public void onProviderDisabled(String provider) {
Log.d("LocationActivity","onProviderDisabled");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("LocationActivity","onStatusChanged");
}
}
|