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

标签 android

我正在回收提供的代码,以在从 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.ConnectionCallbacksGooglePlayServicesClient.OnConnectionFailedListener如果您想使用 new LocationClient(this, this, this); 创建 locationclient

注意:直到 onConnected 之后才尝试使用 locationclient方法在你的回调中被调用。

关于android - 不清楚Android当前位置检索教程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16850569/

相关文章:

Android:如何通过服务发送图片

android - GCM 云连接服务器中上游消息传递的主要用途是什么?

java - 解析 Android HttpResponse 缓存更新?

java - 如何在 Android Studio 中从 SimonVT 导入 MenuDrawer

Android相机预览教程

android - 使用 Dagger 2 的 CustomView 依赖注入(inject)(在 Activity 范围内)

android - Discord bot 在 ios 与 android 和桌面上的功能不同

android - 如何通过 MTP 将 OnePlus Two 设备连接到 Ubuntu?

android - 在 searchview android 中显示 url

java - Android:使用 Apache 服务器将图像上传到 Web 服务时出现问题