0

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

但是有了这个应用程序,我永远不会得到锁,wile 循环只是一直在继续,日志从来没有显示我让它运行了 10 分钟,什么也没有。我在这里遗漏了什么吗?我知道可能会有 > 10 分钟的延迟,但其他应用程序没有使用 lastKnownLocation 而是提取实际位置的问题。

gpsLocation() 方法也在静态定位器类中。

  @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) {} 
  }
4

1 回答 1

1

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

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

所有这一切还假设您已经在清单文件中声明了适当的权限。

尝试这个:

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) 循环,你会阻塞主线程,这通常是一件坏事。

于 2013-11-03T22:03:22.713 回答