问题出在
getLastLocation()
方法上,因为它使用了缓存的位置信息。我曾经尝试过这种简单的方法,但也遇到了同样的问题。后来我改用监听位置更新的方式,并在第一次成功更新后自动停止监听。
下面是我的代码,已经能够正常工作:
首先,在Application中检查可用性(非必要,也可以在Activity中进行,并且不需要保存结果):
public class MainApp extends Application {
public static enum PlayServices {
NOT_CHECKED, AVAILABLE, UNAVAILABLE
};
public static PlayServices mPlayServices = PlayServices.NOT_CHECKED;
@Override
public void onCreate() {
super.onCreate();
if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) == ConnectionResult.SUCCESS) {
MainApp.mPlayServices = MainApp.PlayServices.AVAILABLE;
}
}
}
然后,接下来进入活动:
public class MainActivity extends SherlockFragmentActivity implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener, LocationListener {
在其
onCreate()
中:
if (MainApp.mPlayServices != MainApp.PlayServices.UNAVAILABLE) {
mLocationClient = new LocationClient(this, this, this);
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(5000);
mLocationRequest.setNumUpdates(1);
mLocationRequest.setFastestInterval(1000);
mUpdatesRequested = false;
MainApp.prefs.edit().putBoolean(MainApp.KEY_LOCATION_UPDATES_REQUESTED, mUpdatesRequested)
.commit();
}
其余的MainActivity
类代码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(this.getClass().getSimpleName(), "onActivityResult(" + requestCode + ", " + resultCode
+ ")");
switch (requestCode) {
case MainApp.PLAY_CONNECTION_FAILURE_RESOLUTION_REQUEST:
switch (resultCode) {
case Activity.RESULT_OK:
mLocationClient = new LocationClient(this, this, this);
break;
}
break;
}
}
@Override
public void onConnected(Bundle dataBundle) {
Log.d(this.getClass().getSimpleName(), "onConnected()");
Log.d(this.getClass().getSimpleName(), "Google Play Services are available.");
MainApp.mPlayServices = MainApp.PlayServices.AVAILABLE;
if (!mUpdatesRequested) {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
boolean gps_enabled = false;
try {
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
}
boolean network_enabled = false;
try {
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {
}
MainApp.locEnabled = gps_enabled || network_enabled;
if (!MainApp.locEnabled) {
alertLocationOff();
} else {
mLocationClient.requestLocationUpdates(mLocationRequest, this);
mUpdatesRequested = true;
}
}
}
@Override
public void onDisconnected() {
Log.d(this.getClass().getSimpleName(), "onDisconnected()");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d(this.getClass().getSimpleName(), "onConnectionFailed()");
Log.d(this.getClass().getSimpleName(), "Google Play Services not available.");
MainApp.mPlayServices = MainApp.PlayServices.UNAVAILABLE;
if (connectionResult.hasResolution()) {
try {
connectionResult.startResolutionForResult(this,
MainApp.PLAY_CONNECTION_FAILURE_RESOLUTION_REQUEST);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
} else {
GooglePlayServicesUtil.getErrorDialog(connectionResult.getErrorCode(), this, 0).show();
}
}
@SuppressLint("NewApi")
@Override
public void onLocationChanged(Location location) {
Log.d(this.getClass().getSimpleName(), "onLocationChanged(), location=" + location);
if (location != null) {
boolean present = true;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
present = Geocoder.isPresent();
}
if (present) {
(new ExtractLocationTask(this)).execute(location);
} else {
Log.e(this.getClass().getSimpleName(), "Geocoder not present");
MainApp.mPlayServices = MainApp.PlayServices.UNAVAILABLE;
}
}
}
private class ExtractLocationTask extends AsyncTask<Location, Void, Boolean> {
Context mContext;
public ExtractLocationTask(Context context) {
super();
mContext = context;
}
@Override
protected Boolean doInBackground(Location... params) {
Log.d(getClass().getSimpleName(), "ExtractLocationTask.onPreExecute()");
boolean found = false;
try {
Geocoder geoCoder_local = new Geocoder(mContext, Locale.getDefault());
Geocoder geoCoder_en = new Geocoder(mContext, Locale.ENGLISH);
List<Address> addresses_local = geoCoder_local.getFromLocation(params[0].getLatitude(),
params[0].getLongitude(), 10);
List<Address> addresses_en = geoCoder_en.getFromLocation(params[0].getLatitude(),
params[0].getLongitude(), 10);
if (addresses_local != null && addresses_local.size() > 0) {
MainApp.locEnabled = true;
mUpdatesRequested = false;
MainApp.prefs.edit()
.putBoolean(MainApp.KEY_LOCATION_UPDATES_REQUESTED, mUpdatesRequested).commit();
found = true;
}
} catch (IOException e) {
Log.e(this.getClass().getSimpleName(), "Exception: ", e);
}
return found;
}
@Override
protected void onPostExecute(Boolean found) {
Log.d(getClass().getSimpleName(), "ExtractLocationTask.onPostExecute()");
if (found) {
} else if (!mUpdatesReRequested) {
mLocationClient.requestLocationUpdates(mLocationRequest, (LocationListener) mContext);
mUpdatesRequested = true;
mUpdatesReRequested = true;
}
}
}
我希望这能帮助你使其正常工作!