大家好,小弟想写一个测量跑步距离,平均速度的小应用。想采用gps精确定位的方式实现。
   但是在获取位置时总是失败。网上有人说获取位置时不能保证一次成功,要多试几次。但是我在循环里试了300次,依然是失败的。请高人指点,下面是代码。
package com.android.gpstest;import java.io.IOException;
import java.util.List;
import java.util.Locale;import android.R.anim;
import android.R.bool;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.GpsStatus.Listener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Contacts.Settings;
import android.text.StaticLayout;
import android.util.Log;
import android.widget.Toast;public class MainActivity extends Activity
{
public static final String TAG = "MainActivity"; public static final int MSG_GET_LAST_KNOWN_LOCATION = 0; LocationManager mLocationManager; Criteria mCriteria; @Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main); mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); mCriteria = new Criteria();
mCriteria.setAccuracy(Criteria.ACCURACY_FINE);
mCriteria.setAltitudeRequired(false);
mCriteria.setBearingRequired(false);
mCriteria.setCostAllowed(false);
mCriteria.setPowerRequirement(Criteria.POWER_LOW); if (openGPSSetting())
{
Log.d(TAG, "to set location listener and get last known location");

mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 3000, 0, locationListener);  mHandler.sendEmptyMessageDelayed(MSG_GET_LAST_KNOWN_LOCATION, 100);
}
} static int tryCount = 0;

private Handler mHandler = new Handler()
{
public void handleMessage(Message msg)
{
switch (msg.what)
{
case MSG_GET_LAST_KNOWN_LOCATION:
{
String provider = mLocationManager.getBestProvider(
mCriteria, true); Location location = mLocationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location == null)
{
Log.d(TAG, "get last kown location failed!!!");
tryCount++;
if(tryCount < 300)
{
this.sendEmptyMessageDelayed(MSG_GET_LAST_KNOWN_LOCATION, 50);
}
else
{
tryCount = 0;
}

}
else
{
Log.d(TAG, "get last kown location succeeded"); tryCount = 0;

Toast.makeText(MainActivity.this,
"get location success!", Toast.LENGTH_SHORT)
.show();
updateWithNewLocation(location);
mLocationManager.requestLocationUpdates(provider, 3000,
0, locationListener);
}
}
break; default:
break;
}
}
}; private boolean openGPSSetting()
{
if (mLocationManager
.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER))
{
Toast.makeText(this, "GPS模块正常", Toast.LENGTH_SHORT).show();
Log.d(TAG, "gps is available");
return true;
} Toast.makeText(this, "请开启GPS!", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(
android.provider.Settings.ACTION_SECURITY_SETTINGS); startActivityForResult(intent, 0); return false; } private void updateWithNewLocation(Location location)
{
String latLongString;
String addressString = "没有找到地址\n"; if (location != null)
{
Double geoLat = location.getLatitude() * 1E6;
Double geoLong = location.getLongitude() * 1E6;
latLongString = "经度:" + geoLat + "\n纬度:" + geoLong;
Geocoder gc = new Geocoder(this, Locale.getDefault()); try
{
List<Address> addresses = gc
.getFromLocation(geoLat, geoLong, 1);
StringBuffer sBuffer = new StringBuffer(); if (addresses.size() > 0)
{
Address address = addresses.get(0); for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
{
sBuffer.append(address.getAddressLine(i)).append("\n");
sBuffer.append(address.getLocality()).append("\n");
sBuffer.append(address.getPostalCode()).append("\n");
sBuffer.append(address.getCountryName()).append("\n"); addressString = sBuffer.toString();
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
else
{
Log.d(TAG, "location is null!");
latLongString = "没有找到坐标\n";
}
Toast.makeText(this, latLongString + addressString, Toast.LENGTH_LONG)
.show();

Log.d(TAG, "updateWithNewLocation:" + latLongString + " / " + addressString);
} private final LocationListener locationListener = new LocationListener()
{ @Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
Log.d(TAG, "status changed! provider:" + provider + " status:"
+ status);
} @Override
public void onProviderEnabled(String provider)
{
Log.d(TAG, "provider enabled! provider:" + provider);
} @Override
public void onProviderDisabled(String provider)
{
Log.d(TAG, "provider disabled! provider:" + provider);
} @Override
public void onLocationChanged(Location location)
{
updateWithNewLocation(location);
Log.d(TAG, "location changed! location:" + location.toString());
}
}; @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data); if (openGPSSetting())
{
String provider = mLocationManager.getBestProvider(mCriteria, true); Log.d(TAG, "provider = " + provider); Location location = mLocationManager.getLastKnownLocation(provider); updateWithNewLocation(location); mLocationManager.requestLocationUpdates(provider, 3000, 0,
locationListener);
}
}
}