【问题标题】:Unclear Android Current Location Retrieval Tutorial不清楚Android当前位置检索教程
【发布时间】:2013-05-31 05:51:42
【问题描述】:

我正在回收提供的代码,以在从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。我应该如何/在哪里定义这个?

【问题讨论】:

  • 您的标题与您的描述无关
  • @juned 谢谢,我希望现在更清楚了

标签: android


【解决方案1】:

本教程具有误导性。如果您想检查 google play 服务是否存在,请执行以下操作。

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

如果它不存在,这将自动显示一个适当的错误对话框。

关于你的第二个问题。确实需要遵循本教程的其余部分。如果要使用 new LocationClient(this, this, this); 创建 locationclient,则需要实现 GooglePlayServicesClient.ConnectionCallbacksGooglePlayServicesClient.OnConnectionFailedListener

注意:在您的回调中调用 onConnected 方法之前,请勿尝试使用 locationclient。

【讨论】:

    【解决方案2】:

    按照教程我遇到了同样的错误,但是提供的代码示例似乎已正确实现。

    /**
     * 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));
    
            // Continue
            return true;
        // Google Play services was not available for some reason
        } else {
            // Display an error dialog
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(resultCode, this, 0);
            if (dialog != null) {
                ErrorDialogFragment errorFragment = new ErrorDialogFragment();
                errorFragment.setDialog(dialog);
                errorFragment.show(getSupportFragmentManager(), LocationUtils.APPTAG);
            }
            return false;
        }
    }
    

    http://developer.android.com/shareables/training/LocationUpdates.zip

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多