我正在回收提供的代码,以在从 http://developer.android.com/training/location/retrieve-current.html 获取位置数据之前检查用户设备上是否有 Google Play 服务.将其复制粘贴到我的 IDE 中时,Eclipse 会正确指出行中的错误,因为从未定义过“connectionResult”,也没有定义过“getSupportFragmentManager”
int errorCode = connectionResult.getErrorCode();
和
errorFragment.show(getSupportFragmentManager(),
"Location Updates");
我是否应该在上面创建一个名为 ConnectionResult connectionResult 的变量来解决问题?我不确定如何纠正第二个。
另外,一行
mLocationClient = new LocationClient(this, this, this);
从页面的后面开始,建议放入不满足 LocationClient 构造函数的 MainActivity 类,从而引发另一个错误。
更新:教程的另一个问题。大家好,本教程引用了尚未在此处创建的类 LocationResult:http://developer.android.com/training/location/receive-location-updates.html .我应该如何/在哪里定义这个?
最佳答案
本教程具有误导性。如果您想检查 google play 服务是否存在,请执行以下操作。
int errorCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (errorCode != ConnectionResult.SUCCESS) {
GooglePlayServicesUtil.getErrorDialog(errorCode, this, 0).show();
}
如果它不存在,这将自动显示一个适当的错误对话框。
关于你的第二个问题。确实需要遵循本教程的其余部分。您确实需要实现 GooglePlayServicesClient.ConnectionCallbacks
和 GooglePlayServicesClient.OnConnectionFailedListener
如果您想使用 new LocationClient(this, this, this);
创建 locationclient
注意:直到 onConnected
之后才尝试使用 locationclient方法在你的回调中被调用。
关于android - 不清楚Android当前位置检索教程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16850569/