How do I find out if the GPS of an Android device is enabled

后端 未结 10 1805
暖寄归人
暖寄归人 2020-11-22 08:36

On an Android Cupcake (1.5) enabled device, how do I check and activate the GPS?

10条回答
  •  无人共我
    2020-11-22 09:12

    In Kotlin: How to check GPS is enable or not

     val manager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
            if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                checkGPSEnable()
            } 
    
     private fun checkGPSEnable() {
            val dialogBuilder = AlertDialog.Builder(this)
            dialogBuilder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
                    .setCancelable(false)
                    .setPositiveButton("Yes", DialogInterface.OnClickListener { dialog, id
                        ->
                        startActivity(Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS))
                    })
                    .setNegativeButton("No", DialogInterface.OnClickListener { dialog, id ->
                        dialog.cancel()
                    })
            val alert = dialogBuilder.create()
            alert.show()
        }
    

提交回复
热议问题