android 上如何判断是否已获取GPS位置?

发布于 2024-11-05 06:45:17 字数 115 浏览 0 评论 0原文

假设我有一个按钮,仅当系统知道确切的用户位置时才会调用另一个活动。我创建了一个对话框,要求用户在禁用 GPS 的情况下打开它,但是当用户启用 GPS 并再次按下按钮时,我可能还没有确切的位置。谁能帮我解决这个问题吗?

Let's say I have a button which calls another activity only if the system knows the EXACT user's location. I made a dialog to ask the user to turn on GPS if it's disabled, but when the user enables GPS and pushes the button again, I will probably not have the exact location yet. Can anyone help me with this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

书信已泛黄 2024-11-12 06:45:17

您需要创建一个LocationListener,并等待回调。 api docs 对此进行了很好的描述:

// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
  public void onLocationChanged(Location location) {
     // When you get here you have a location...
     useTheNewLocationToEnableTheButton(location); 
  }

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

  public void onProviderEnabled(String provider) {}

  public void onProviderDisabled(String provider) {}
};

// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

You need to create a LocationListener, and wait for the callback. It is pretty well described in the api docs:

// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
  public void onLocationChanged(Location location) {
     // When you get here you have a location...
     useTheNewLocationToEnableTheButton(location); 
  }

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

  public void onProviderEnabled(String provider) {}

  public void onProviderDisabled(String provider) {}
};

// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
梦醒灬来后我 2024-11-12 06:45:17

您可以监听 GpsStatus 事件,以确保用户确实启用了 GPS (GPS_EVENT_STARTED),并在 GPS 获得定位时收到通知 (GPS_EVENT_FIRST_FIX)。

You can listen for GpsStatus events, to make sure that user actually enabled GPS (GPS_EVENT_STARTED), and to be notified when GPS gets position fix (GPS_EVENT_FIRST_FIX).

踏月而来 2024-11-12 06:45:17

如果您不知道坐标,最好的方法是显示 ProgressDialog 在检索位置时,获取位置后,关闭 ProgressDialog 并启动您的活动。

final LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
  public void onLocationChanged(Location location) {
    //here you can get coordinates via location.getLongitude() and
    //location.getLatitude() methods, dismiss the progress dialog and
    //start your activity
    locationManager.removeUpdates(this);
  }
  public void onStatusChanged(String provider, int status, Bundle extras) {}
  public void onProviderEnabled(String provider) {}
  public void onProviderDisabled(String provider) {}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

If you don't know coordinates, the best way is to display a ProgressDialog while retrieving a location and, when you got it, dismiss ProgressDialog and launch your activity.

final LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
  public void onLocationChanged(Location location) {
    //here you can get coordinates via location.getLongitude() and
    //location.getLatitude() methods, dismiss the progress dialog and
    //start your activity
    locationManager.removeUpdates(this);
  }
  public void onStatusChanged(String provider, int status, Bundle extras) {}
  public void onProviderEnabled(String provider) {}
  public void onProviderDisabled(String provider) {}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文