简体   繁体   English

如何强制Android重新读取GPS位置?

[英]How enforce Android to re-read GPS position?

I try to write application, which shows GPS coordinates on button click. 我尝试编写应用程序,该应用程序在单击按钮时显示GPS坐标。

@Override
public void onClick(View arg0) {
    EditText editText;
    editText = (EditText) findViewById(R.id.editText1);
    LocationManager locationManager;
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    Location location = locationManager.getLastKnownLocation("gps");
    editText.setText(location.toString());
}

Is problem: while in first time I got coordinates of location with time of GPS reading, second click get the same location. 问题:第一次使用GPS读取时间获得位置坐标时,第二次单击获得相同位置。 Even when I stop program and run again, it get the same location! 即使我停止程序并再次运行,它也会获得相同的位置! How enforce to refresh reading GPS? 如何强制刷新阅读GPS?

while in first time I got coordinates of location with time of GPS reading, second click get the same location 第一次我得到的位置坐标与GPS读取时间有关,第二次单击则获得相同的位置

That is not surprising. 这不足为奇。

Even when I stop program and run again, it get the same location! 即使我停止程序并再次运行,它也会获得相同的位置!

That too is not surprising. 这也不足为奇。

How enforce to refresh reading GPS? 如何强制刷新阅读GPS?

Generally speaking, you don't. 一般来说,您不需要。 There is no requirement that the device has to have a fresh update just because you request the last-known location. 不需要仅因为您请求最后一个已知位置就必须对设备进行全新更新。 It may be physically impossible to provide a new location fix. 提供新的位置定位信息在物理上可能是不可能的。 And, if you are not calling requestLocationUpdates() , it is very possible for getLastKnownLocation() to simply return null . 而且,如果您不调用requestLocationUpdates() ,则getLastKnownLocation()很可能仅返回null

Your parameters to requestLocationUpdates() may have some impact for how aggressively the device tries to update GPS, versus saving power. 您请求requestLocationUpdates()参数可能会影响设备尝试更新GPS的积极程度,而不是节省电量。 However, there are no guarantees. 但是,没有任何保证。

Try to get best provider that available to fetch the Location data , using this code before you get the location 在获取位置之前,请尝试使用此代码来获取可用于获取位置数据的最佳提供商。

Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, false);

and in your getLastKnownLocation pass the best Location Provider to get the location like this 并在您的getLastKnownLocation传递最佳的位置提供程序来获取这样的位置

 Location location = locationManager.getLastKnownLocation(bestProvider );

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

相关问题 Kafka与Java:如何重新读取数据 - Kafka with Java: how to re-read data 如何重置偏移值重新读取kafka主题 - How to reset the offset value to re-read the kafka topic 调用IOUtils.copy后如何重新读取InputStream? - How to re-read an InputStream after calling IOUtils.copy? 无需重启即可重新读取配置 - Re-read configuration without restart 如何读取文件内容,写入outputstream以及重新读取并存储在Java内存中的字符串变量中 - how to read contents of file, write to outputstream and the re-read and Store in a string variable in memory in java 内容更改后如何重新读取文本文件的新内容? - How can I re-read the new contents of a text file after the contents have changed? 如何将字符串中的新行字符插入PrintStream,然后使用扫描程序重新读取该文件 - how to insert a new line character in a string to PrintStream then use a scanner to re-read the file 如何将字符串保存在本地缓冲区中,以便扫描程序可以重新读取它? - How to save a String in local buffer so that it can be re-read by Scanner? 如何创建一个休眠集合,每次我请求时都将重新读取它? - How can I create a hibernate collection that will be re-read every time I request it? FutureTask 实现重新读取 state 以防止泄漏中断 - FutureTask Implementation re-read state to prevent leaked intterupts
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM