简体   繁体   中英

Using Fused Location provider for android in a non activity java class and get longitude and latitude in main Activity class

I want to use fused location provider in my android application and get the current users location longitude and latitude and pass it as a map link in text message.

I used this example FusedLocation Example to get long and lat coordinates of location but in my case i want to put all fusedLocation functions in a java class which is a non activity class and call it from main activity class to get long and lat ,

since ima beginner in android and i searched alot to find something like this but couldn't get a example or tutorial.

can anybody help me in this or advice me if its the better solution to retrieve location coordinates and address from any provider working in any situations?
thanks in advance

That is quite possible - in fact you can take a look at this Gist that I created. Essentially, the "non-Activity" class that you use, should implement the Location-Service Connection Listeners GoogleApiClient.ConnectionCallbacks , GoogleApiClient.OnConnectionFailedListener (or receive instance of GoogleApiClient as parameter), and also the LocationListener -you may also want to pass the Context object to the constructor of that class (which will be the activity's context). Here is Gist which provides an example of implementing a LocationListener .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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