How to know GPS Location without 'onLocationChange' method

白昼怎懂夜的黑 提交于 2019-12-10 16:14:15

问题


I want to send an SMS on click of one button and in the SMS i want to send location information. i tried:

 location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

but for the first time it shows null because no known location found. Hence i have to wait to fire 'onLocationChange' of the LocationListener, but i dont want that.

Please tell me some other way so that i can get location at one shot. thanks.


回答1:


Acquiring GPS coordinates the first time (if GPS Service is not running) can be a time-intensive process because the GPS needs to do a lot initializing. The firing of the first onLocationChange is a way of Android's GPS to say: "I know where I am now." So you might have to figure out a way to work with onLocationChange.

The way to you use your SMS/GPS API as on demand is to the GPS service is running constantly. Have you looked into/tried that? You could also wait in your SMS process until a GPS location is available, until some reasonable-length timeout event occurs, and then send the SMS. And subsequent requests should go much more quickly since you have your GPS initialized. In any case, you cannot count on the lat/long from the GPS Service in real time.

Here's a great article on mobile GPS. It's general but reading through it will reveal the reasons why you cannot simply get a GPS reading "at the touch of a button."



来源:https://stackoverflow.com/questions/5233264/how-to-know-gps-location-without-onlocationchange-method

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!