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

后端 未结 3 818
执笔经年
执笔经年 2021-02-14 04:06

I have the following code and it\'s not good because sometimes GPS takes very long

How can I do the following:

  1. Check if GPS is enabled
  2. Use GPS if
相关标签:
3条回答
  • 2021-02-14 04:10

    Just using this, you can check GPS availability

        LocationManager mlocManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);;
        boolean enabled = mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    
    0 讨论(0)
  • 2021-02-14 04:10

    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...

    Hope this helps.

    Regards!

    0 讨论(0)
  • 2021-02-14 04:19

    Think you could use the code in my answer to: Location servise GPS Force closed

    It gives you a callback method for GPS first fix and location changes that can be very convenient. 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.

    0 讨论(0)
提交回复
热议问题