简体   繁体   English

在尝试使用GPS之前,如何检查GPS是否已启用

[英]How can I check if GPS is enabled before I try to use it

I have the following code and it's not good because sometimes GPS takes very long 我有以下代码,但效果不好,因为有时GPS需要很长时间

How can I do the following: 如何执行以下操作:

  1. Check if GPS is enabled 检查是否启用了GPS
  2. Use GPS if it is enabled otherwise use the network provider. 如果启用了GPS,请使用GPS,否则请使用网络提供商。
  3. If GPS takes more than 30 seconds, use network. 如果GPS需要30秒以上,请使用网络。

I can do this with my own logic using a time or Thread.sleep but I think there might be a more stabndard way 我可以使用时间或Thread.sleep以自己的逻辑进行此操作,但我认为可能会有更稳定的方法

// 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) {
                  // Called when a new location is found by the network location provider.
                    locationCallback(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);

Just using this, you can check GPS availability 只需使用此功能,即可查看GPS可用性

    LocationManager mlocManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);;
    boolean enabled = mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

There's no standard way, you have to do it on your own with the help of: 没有标准的方法,您必须借助以下方法自行完成:

    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
        //Do what you need if enabled...
    }else{
        //Do what you need if not enabled...
    }

And this permission in manifest: 并且此权限在清单中:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

As recommendation if GPS is not enabled, usually the standard specifies to popup the Location Settings Activity so the user can specifically enable it... 作为建议,如果未启用GPS,通常标准会指定弹出“位置设置活动”,以便用户可以专门启用它。

Hope this helps. 希望这可以帮助。

Regards! 问候!

Think you could use the code in my answer to: Location servise GPS Force closed 认为您可以在我的答案中使用以下代码: 位置服务GPS强制关闭

It gives you a callback method for GPS first fix and location changes that can be very convenient. 它为您提供了一种用于GPS首次定位和位置更改的回调方法,该方法非常方便。 This also makes it easy to change the implementation of GPSTracker to switch to network if GPS takes too long to get a first fix. 如果GPS花费太长时间无法获得首次修复,这也可以轻松更改GPSTracker的实现以切换到网络。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我向用户显示了启用gps设置的提示,但是如何检查用户是否在手机上真正启用了gps位置? - i showed prompt to user to enable gps settings,but how can i check user really enabled the gps location on his phone? 如何在应用程序中检查GPS支持以为启用了定位服务的用户添加功能? - How can I check for GPS support in-App to add a feature for those with Location services enabled? 我如何检查用户是否在Android手机上真正启用了GPS位置 - How can i check user really enabled the gps location on his phone in android 如何检查Android GPS是否仍在扫描或位置固定? 不只是检查是否启用 - How do I check if Android GPS is still scanning or position is fixed? not just check if enabled 如何检查gmail是否已启用 - How can I check if gmail is enabled or disabled 如何为 gps 使用特定对话框? - How can I use a specific dialog for gps? 在使用手机前,如何检查手机中是否启用了Google Instant Apps功能? - How can I check whether the Google Instant Apps feature is enabled in a phone before using it? phonegap:如何检查是否启用了gps - phonegap: how to check if gps is enabled 如何查看GPS接收器的当前状态? - How can I check the current status of the GPS receiver? 如何查看手机是否有 GPS 设备? - How can I check whether the phone has GPS device or not?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM