简体   繁体   English

活动识别和位置客户端Android

[英]Activity Recognition and Location Client Android

I am having difficulty making use of both Activity Recognition an LocationClient to fetch current location. 我很难同时使用两个Activity Recognition和LocationClient来获取当前位置。

I want to fetch users current location when the Activity recognition returns "IN_VEHICLE" 我想在活动识别返回“ IN_VEHICLE”时获取用户的当前位置

But when i try to connect my location client it gives me error like this 但是当我尝试连接我的位置客户端时,它给了我这样的错误

 Call connect() and wait for onConnected

Any idea on how can I get users current location using activity recognition ? 关于如何使用活动识别获取用户当前位置的任何想法?

I hope my answer is correct :) 我希望我的答案是正确的:)

First, the error you're getting is, as I'm guessing without much code/context, because you're trying to do something with your location client before it has actually connected to Location Services, hence it's asking you to wait for it to be connected and then proceed with what you want. 首先,由于我猜测没有太多代码/上下文,因此您遇到的错误是因为您试图在位置客户端实际连接到定位服务之前对其进行操作,因此它要求您等待它连接,然后继续进行所需的操作。

As mentioned in the docs - OnConnected() : 如文档中所述-OnConnected():

After calling connect(), this method will be invoked asynchronously when the connect request has successfully completed . 调用connect()之后,当连接请求成功完成时, 异步调用此方法。 After this callback, the application can make requests on other methods provided by the client [...] 此回调之后,应用程序可以对客户端提供的其他方法进行请求[...]

To do something else once it's connected you might set a flag in your activity - isConnected - or you can just use mLocationClient.isConnected() which will return if it's connected or not. 要在连接后执行其他操作,您可以在活动中设置一个标志-isConnected-或只使用mLocationClient.isConnected()即可,无论是否连接,它都会返回。

Regarding your question , just with activity recognition you shouldn't be able to get the location, just the state (activity). 关于您的问题 ,仅凭活动识别,您就不应仅获得状态(活动)就可以获取位置。 But you can still use both LocationClient and ActivityRecognition. 但是您仍然可以同时使用LocationClient和ActivityRecognition。

Keep an eye that LocationClient and ActivityRecognition have been deprecated and you should now use GoogleApiClient. 请注意,LocationClient和ActivityRecognition已被弃用,您现在应该使用GoogleApiClient。 Something like (as shown in https://developer.android.com/google/auth/api-client.html ): 类似于(如https://developer.android.com/google/auth/api-client.html所示):

GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

As stated in the docs, you can add multiple APIs to this GoogleApiClient, so you could either have just one client using both LocationServices.API and ActivityRecognition.API, or maybe have different clients, each with an API and work separately. 如文档所述,您可以向此GoogleApiClient添加多个API,因此您可以只有一个同时使用LocationServices.API和ActivityRecognition.API的客户端,或者可以有不同的客户端,每个客户端都具有一个API并分别工作。

Hope this works ! 希望这有效! Let me know if something's not clear 让我知道是否不清楚

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM