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

后端 未结 3 1857
鱼传尺愫
鱼传尺愫 2021-02-14 03:27

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:34

    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:

    
    

    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!

提交回复
热议问题