Android Google Play 服务基于位置的 API 工作流程

标签 android location google-play-services location-aware

我是 android 编程的新手。我打算制作一个位置敏感的应用程序。

我正在尝试使用 Google Play 服务的基于位置的 API 来检测用户的当前位置。为此,我一直在关注示例代码和此处给出的详细信息:-

developer.android.com- detect user's current location- Location APIs- Google Play Services

我试图了解检测用户当前位置所涉及的不同方法调用之间的联系。现在上面的链接虽然看起来有很多信息,但作为新手,我发现很难在这里连接不同的方法调用。

所以我大致理解上面 URL 中的代码是这样的:-

  1. 在 MainActivity 类的 onCreate() 中,我们创建了一个 LocationClient。 LocationClient 用于连接定位服务。
  2. 现在在 MainActivity 类的 onStart() 中,我们调用 locationClientObjet.connect()。这意味着只要应用的 MainActivity 可见,就会尝试连接到位置服务。
  3. 现在,在我们制作 LocationClient 之前,我们已经定义了称为位置服务回调的东西。
  4. 因此我们实现了所需的接口(interface)并定义了各自的方法。因此,例如,当连接客户端的请求成功完成时,将调用 onConnected() 。

Now so far everything sounds good. The problem arises when I try to connect the above part with the below mentioned :-

  1. 现在,甚至在我们定义位置服务回调之前,我们已经在 MainActivity 类中定义了一个名为 ErrorDialogFragment 的内部类。我猜这个类是用来创建一个错误 fragment (比如一个警告框),它可以向用户显示错误消息,以防显然由于某种原因连接到定位服务的尝试失败。所以这里有些事情我不明白:-
  • What is the use of CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; The documentation in the page says :- Define a request code to send to Google Play services. This code is returned in Activity.onActivityResult. I do not understand this.
  • When is the onCreateDialog() inside the ErrorDialogFragment class get called. I do not find an explicit call to this method anywhere in the sample code mentioned on the page in the URL mentioned above.
  • What is the connection between the onConnectionFailed() and the error fragment.
  • There are 2 more methods defined :- onActivityResult() and servicesConnected(). I understand to some extent the use of servicesConnected() - it is used to see if the GooglePlay services is available. Is it a user defined method ? And is it not ding the same things that were being done inside the callback methods onConnected(), onDisConnected() and onConnectionFailed(). If not how are they different from servicesConnected() ?
  • And finally I just do not understand what is the use of onActivityResult(), what exactly are we trying to do here ?

请原谅我的无知。我是 Android 编程的新手,正在努力学习清晰可靠的概念。请纠正我哪里出错或误解的地方。我尝试查看 Vogella 资源,但找不到太多帮助。任何解释细节的好资源都会对我的上述概念有很大帮助。

最佳答案

Q : What is the use of CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; The documentation in > the page says :- Define a request code to send to Google Play services. This code is > returned in Activity.onActivityResult. I do not understand this.

A : CONNECTION_FAILURE_RESOLUTION_REQUEST 是你定义的请求码,如果MainActivity退出,onActivityResult得到请求码,一开始。 IE 。 CONNECTION_FAILURE_RESOLUTION_REQUEST(或 USER_DEFINED_REQUEST_CODE) 这是由 startActivityForResult(Intent,) 指定的。这里是通过

connectionResult.startResolutionForResult(this,CONNECTION_FAILURE_RESOLUTION_REQUEST);

它本身在内部调用 startActivityForresult

用法在定义onActivityResult之前以注释的形式在给定的示例代码'MainActivity.java'中指定。此处引用如下:

/* * 处理其他 Activity 返回给这个 Activity 的结果 * 启动 Activity 结果()。特别是 onConnectionFailed() 中的方法 * LocationUpdateRemover 和 LocationUpdateRequester 可以调用 startResolutionForResult() 来 * 启动处理 Google Play 服务问题的 Activity。这的结果 * 调用返回到 onActivityResult。 */

Q : When is the onCreateDialog() inside the ErrorDialogFragment class get called. I do not > find an explicit call to this method anywhere in the sample code mentioned on the page in > the URL mentioned above.

答:从这里调用:

//在DialogFragment中显示错误对话框

errorFragment.show(getSupportFragmentManager(),
                    "Location Updates");

onCreateDialog 将对话框添加到对话框缓存中,show 方法调用它。

Q: What is the connection between the onConnectionFailed() and the error fragment.

A: onConnectionFailed 是一个回调方法,当客户端连接到 google play 服务出错时调用。可以在以下位置找到错误列表 http://developer.android.com/reference/com/google/android/gms/common/ConnectionResult.html在“摘要”部分。

现在如果错误发生并且有一些解决方案,错误 fragment 将尝试解决它。看着 http://developer.android.com/reference/com/google/android/gms/common/ConnectionResult.html#hasResolution()http://developer.android.com/reference/com/google/android/gms/common/ConnectionResult.html#startResolutionForResult(android.app.Activity , 整数)

这里的“this”指的是 MainActivity 和 CONNECTION_FAILURE_RESOLUTION_REQUEST 是由 startActivityForResult 调用的用户定义的请求代码,在这里由

connectionResult.startResolutionForResult(
                        this,
                        CONNECTION_FAILURE_RESOLUTION_REQUEST);.

Q: There are 2 more methods defined :- onActivityResult() and servicesConnected(). I understand to some extent the use of servicesConnected() - it is used to see if the GooglePlay services is available. Is it a user defined method ? And is it not ding the same things that were being done inside the callback methods onConnected(), onDisConnected() and onConnectionFailed(). If not how are they different from servicesConnected() ?

A:是的,servicesConnected 是一个用户定义的方法。

恐怕不一样,它通过 isGooglePlayServicesAvailable() 检查 google play 服务是否可用。

另一方面,OnConnected() 将在 isGooglePlayServicesAvailable 返回 true 后调用。所以他们在服务连接或断开连接后采取行动,servicesConnected() 只是检查它是否连接。

Q: And finally I just do not understand what is the use of onActivityResult(), what exactly are we trying to do here ?

我希望从之前的回答中可以清楚地看出这一点 :)

关于Android Google Play 服务基于位置的 API 工作流程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23368524/

相关文章:

android - 如何取消应用程序与谷歌驱动器帐户的链接

android - android 中过度滚动的视觉指示

android - 如何为线性布局元素设置 setLayoutParams

java - 在 playframework 应用程序中查找 IP 地址

android - 使用 PendingIntent 获取位置始终返回 Null

c# - 推出 Beta 版时的 Google Play 游戏服务 : ERROR_NOT_AUTHORIZED,

Android - HttpClient 只工作一次

java - 使用 java cv 时在 android studio 上出现构建错误

ios - 使用 CLLocationManager 减少获取位置的频率

android - 当 stagerollout 到 100% 时,应用程序不会在开发人员控制台上更新