【发布时间】:2014-09-20 23:16:44
【问题描述】:
我正在尝试收听位置变化,但有时从未调用过 onLocationChanged 回调,而 getLastLocation 返回 null,而 Google 地图始终运行良好。
注意
如果我重新启动我的设备,定位服务将只能工作约 2 天;之后,我的应用程序和 the SDK example 都将无法运行,而 Google 地图仍在运行。
这是我的服务代码:
public class VisitService extends Service implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener,
LocationListener {
private final IBinder mBinder = new VisitBinder();
// A request to connect to Location Services
private LocationRequest mLocationRequest;
// Stores the current instantiation of the location client in this object
private LocationClient mLocationClient;
private boolean mLocationServiceConnected = false;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
initLocation();
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
initLocation();
return mBinder;
}
public void startVisit() {
if (!servicesConnected()) {
listener.onVisitStartError();
return;
}
if (mLocationServiceConnected) {
if (isAcceptableLocation(mLocationClient.getLastLocation())) {
Toast.makeText(this, "You have arrived!", Toast.LENGTH_LONG);
} else {
mVisitListener.onVisitStartError();
}
}
}
private boolean isAcceptableLocation(Location location) {
if (location == null) {
Toast.makeText(this, "Location is null", Toast.LENGTH_LONG).show();
return false;
}
return true;
}
private void initLocation() {
if (mLocationRequest == null) {
// Create a new global location parameters object
mLocationRequest = LocationRequest.create();
/*
* Set the update interval
*/
mLocationRequest
.setInterval(LocationUtils.UPDATE_INTERVAL_IN_MILLISECONDS);
// Use high accuracy
mLocationRequest
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
// Set the interval ceiling to one minute
mLocationRequest
.setFastestInterval(LocationUtils.FAST_INTERVAL_CEILING_IN_MILLISECONDS);
}
if (mLocationClient == null) {
mLocationClient = new LocationClient(this, this, this);
mLocationClient.connect();
}
}
/**
* Verify that Google Play services is available before making a request.
*
* @return true if Google Play services is available, otherwise false
*/
private boolean servicesConnected() {
// Check that Google Play services is available
int resultCode = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(this);
// If Google Play services is available
if (ConnectionResult.SUCCESS == resultCode) {
// In debug mode, log the status
Log.d(LocationUtils.APPTAG,
getString(R.string.play_services_available));
return true;
} else {
return false;
}
}
public class VisitBinder extends Binder {
public VisitService getService() {
return VisitService.this;
}
}
/**
* In response to a request to start updates, send a request to Location
* Services
*/
private void startPeriodicUpdates() {
mLocationClient.requestLocationUpdates(mLocationRequest, this);
Toast.makeText(this, "Location Update Requested", Toast.LENGTH_LONG).show();
}
/**
* In response to a request to stop updates, send a request to Location
* Services
*/
private void stopPeriodicUpdates() {
mLocationClient.removeLocationUpdates(this);
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d(LocationUtils.APPTAG, "Location Services Connection faild");
}
@Override
public void onConnected(Bundle bundle) {
Log.d(LocationUtils.APPTAG, "Location Services Connected");
mLocationServiceConnected = true;
startPeriodicUpdates();
}
@Override
public void onDisconnected() {
mLocationServiceConnected = false;
}
@Override
public void onLocationChanged(Location location) {
Toast.makeText(this, "Location has been changed", Toast.LENGTH_LONG).show();
}
}
Android 清单
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
【问题讨论】:
-
来自文档:
If a location is not available, which should happen very rarely, null will be returned。似乎您正在点击rarely时刻,可能是由于设备。您是否尝试从LocationManager获取最后知道的位置? -
我还会请求“ACCESS_COARSE_LOCATION”,以防找不到合适的位置。
-
您可以等待您的
onLocationChanged回调被调用,然后再调用您的startVisit()方法。例如,你可以这样,不是吗? “我无法收到 onLocationChanged”到底是什么意思? -
@MagicMicky 的意思是
onLocationChanged永远不会被播放位置服务调用。 -
@Robert 我尝试添加 ACCESS_COARSE_LOCATION 权限,但没有任何改变。
标签: java android google-play-services