gpt4 book ai didi

java - 应用程序从未收到 GPS 修复

转载 作者:行者123 更新时间:2023-12-01 13:53:44 25 4
gpt4 key购买 nike

我们有很强的 GPS 信号,我有一个应用程序可以测试建立 GPS 锁定的速度,我什至在我的手机上测试了导航应用程序,如果我打开 GPS,它们几乎都会获得低于 10 的 insta 锁定。

但是使用这个应用程序,我永远不会获得锁定,wile 循环会一直持续下去,并且日志从未显示我让它运行了 10 分钟,但什么也没有。我是否在这里遗漏了一些东西,我知道可能会有> 10分钟的延迟,但其他应用程序没有问题,不使用lastKnownLocation而是提取实际位置。

此外,gpsLocation() 方法位于静态 Locator 类中。

  @Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

Location location = Locator.gpsLocation(this);

while(location == null)
{
location = Locator.gpsLocation(this);
}

Log.w("GPS LOCATION", "LOCATION FOUND");


}

/**
* Finds users location using gps
* satelite network.
*
* @param FragmentActivity
* @return Location
*/
public static Location gpsLocation(FragmentActivity context)
{
// Fetch LocationManager service & instance of UserLocator
LocationManager provider = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
UserLocator locator = new UserLocator();

// Attempt to get a fix on users location
provider.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locator);

// Return users location using GPS Service
return locator.location;
}

/**
* Use to listen for user location updates
* TODO: Reimplement as a anonymous class
*/
class UserLocator implements LocationListener
{
// Public accessor
public Location location;
public void onLocationChanged(Location location)
{
if(location != null)
{
// We have a fix on users location
this.location = location;
}
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
}

最佳答案

我认为您的问题是您的位置更新监听器正在更新 UserLocation 类中的位置变量,但是您正在检查的变量是从 gpsLocation 方法返回的变量,该变量为 null。

您正在做的是,您正在注册位置更新并立即返回 null 值并一遍又一遍地检查它,而您的真实位置正在另一个地方更新。

所有这一切都假设您已在 list 文件中声明了适当的权限。

试试这个:

Location location;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
gpsLocation(this);
while(location == null)
continue;
Log.w("GPS LOCATION", "LOCATION FOUND");
}


public static void gpsLocation(FragmentActivity context)
{

LocationManager provider = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
UserLocator locator = new UserLocator();
provider.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locator);
}


class UserLocator implements LocationListener
{
public void onLocationChanged(Location loc)
{
if(loc != null)
location = loc;

}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
}

通过此实现,监听器和 while 循环都会寻址相同的 Location 实例,因此一旦该实例从 null 更改,您将收到日志消息。

但是,我强烈建议您不要在 onCreate 方法中放置这样的 while(true) 循环,否则您将阻塞主线程,这通常是一件坏事。

关于java - 应用程序从未收到 GPS 修复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19758397/

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