gpt4 book ai didi

android - 即使我坐在同一个地方,也会调用 onlocationChanged

转载 作者:太空宇宙 更新时间:2023-11-03 11:44:30 32 4
gpt4 key购买 nike

我使用了下面的代码并且一切正常,除了调用 onLocationChanged,即使我坐在同一个位置。

我认为它应该只在我移动时调用,对吗?

我只想在移动一定距离后获取位置。

请帮帮我。提前致谢。

 @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

locationMgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

locationMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
this);


}

@Override
public void onLocationChanged(Location location) {
Toast.makeText(this, "Working!", Toast.LENGTH_SHORT).show();
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();

String Text = "Latitud = " + lat + "\nLongitud = " +
lng;

Toast.makeText(getBaseContext(),Text,Toast.LENGTH_SHORT).show();
}
}

最佳答案

您请求以尽可能短的间隔/距离更新位置

locationMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
this);

这是文档中关于这些参数的内容

" The location update interval can be controlled using the minTime parameter. The elapsed time between location updates will never be less than minTime, although it can be more depending on the Location Provider implementation and the update interval requested by other applications. "

The minDistance parameter can also be used to control the frequency of location updates. If it is greater than 0 then the location provider will only send your application an update when the location has changed by at least minDistance meters, AND at least minTime milliseconds have passed. However it is more difficult for location providers to save power using the minDistance parameter, so minTime should be the primary tool to conserving battery life.

我个人在我的应用中使用的 minTime 为 10 秒和 10 米

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000,
10, locationListener);

关于android - 即使我坐在同一个地方,也会调用 onlocationChanged,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14569268/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com