gpt4 book ai didi

android - 如何检查 GPS 是否已连接以及位置是否已更新

转载 作者:行者123 更新时间:2023-11-29 17:39:18 25 4
gpt4 key购买 nike

GPS 需要连接到 GPS 卫星才能实时跟踪位置。问题是有时它可能会失去连接,如果我调用

    locationManager.getLastKnownLocation(provider);

我得到可能与实际位置不同的最后已知位置。如何在调用/使用 getLastKnownLocation 之前检查 GPS 是否已连接以及位置是否已更新?

最佳答案

实现GpsStatus.Listener ActivityService 上的接口(interface):

public class LocationActivity extends Activity implements GpsStatus.Listener
{

private LocationManager locationManager;

@Override
public void onCreate(Bundle savdedInstanceState)
....
....
locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
locationManager.addGpsStatusListener(this);
}

@Override
public void onGpsStatusChanged(int event)
{

switch (event)
{
case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
break;
case GpsStatus.GPS_EVENT_FIRST_FIX:
/* This is when GPS is activated; add code here for how you want the application to react. */
break;
case GpsStatus.GPS_EVENT_STARTED:
break;
case GpsStatus.GPS_EVENT_STOPPED:
break;
}
}
}

关于android - 如何检查 GPS 是否已连接以及位置是否已更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29318002/

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