不清楚Android当前位置检索教程

Num*_*ife 16 android

我正在回收所提供的代码,以便在从http://developer.android.com/training/location/retrieve-current.html获取位置数据之前检查用户的设备上是否有Google Play服务.将它复制粘贴到我的IDE中时,Eclipse正确地指出了行中的错误,因为"connectionResult"从未定义过,"getSupportFragmentManager"也没有定义

int errorCode = connectionResult.getErrorCode();
Run Code Online (Sandbox Code Playgroud)

errorFragment.show(getSupportFragmentManager(),
                    "Location Updates");
Run Code Online (Sandbox Code Playgroud)

我应该只创建一个名为ConnectionResult connectionResult的变量来解决问题吗?我不确定如何纠正第二个问题.

另外,这条线

mLocationClient = new LocationClient(this, this, this);
Run Code Online (Sandbox Code Playgroud)

从页面的更进一步建议放入不满足LocationClient构造函数的MainActivity类,引起另一个错误.

更新:本教程的另一个问题.大家好,这个教程引用了它没有在这里创建的类LocationResult:http://developer.android.com/training/location/receive-location-updates.html.我应该如何/在哪里定义这个?

Lio*_*ort 35

该教程具有误导性.如果您想检查谷歌播放服务存在,请执行以下操作.

int errorCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (errorCode != ConnectionResult.SUCCESS) {
  GooglePlayServicesUtil.getErrorDialog(errorCode, this, 0).show();
}
Run Code Online (Sandbox Code Playgroud)

如果不存在,这将自动显示相应的错误对话框.

对你的第二个问题.确实需要遵循本教程的其余部分.您需要实现GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener如果要使用创建locationclientnew LocationClient(this, this, this);

注意:onConnected在回调中调用方法之前,不要尝试使用locationclient .