モバイルデバイスでGPSの緯度と経度を取得しようとしています。ログにエラーはありませんが、情報を取得できません。「ワイヤレスネットワーク/GPSを使用する」設定が選択されているかどうかは関係ありません。ヘルプが必要です
public class MainActivity extends Activity {
private final static String TAG = "GPSTest";
private LocationManager mLocationManager;
private LocationListener mLocationListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String serviceName = Context.LOCATION_SERVICE;
mLocationManager = (LocationManager) getSystemService(serviceName);
mLocationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
Log.e(TAG, "onLocationChanged");
if (location != null) {
Log.e(TAG, "Current Latitude = " + location.getLatitude());
Log.e(TAG, "Current Longitude = " + location.getLongitude());
}
mLocationManager.removeUpdates(this);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Log.e(TAG, "onProviderDisabled");
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Log.e(TAG, "onProviderEnabled");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
Log.e(TAG, "onStatusChanged");
}
};
if (!mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000, 10, mLocationListener);
} else {
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, mLocationListener);
}
}
@Override
protected void onDestroy() {
mLocationManager.removeUpdates(mLocationListener);
super.onDestroy();
}
}