当我覆盖函数onRequestPermissionsResult不起作用

时间:2019-04-12 19:29:08

标签: android kotlin permissions

当我重写函数onRequestPermissionsResult不起作用时。

当我删除覆盖命令Android Studio时,不再抱怨,但该功能仍然无用... 我正在请求ACCES_FINE_LOCATION的权限

错误消息是“修饰符'override'不适用于'local function'”

class MainActivity : AppCompatActivity()  {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
val texto = TextView(this)
val ID_REQUISICAO_ACCESS_FINE = 1
setContentView(texto)


override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
    when (requestCode) {
           ID_REQUISICAO_ACCESS_FINE -> {
    // If request is cancelled, the result arrays are empty.
     if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    // permission was granted, yay! Do the location-related task you need to do.
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
 Toast.makeText(this, "permission granted", Toast.LENGTH_LONG).show()
                            }
  } else {
// permission denied, boo! Disable the functionality that depends on this permission.
Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show()
                        }
                        return
                    }
                }
                }

            }
        }

1 个答案:

答案 0 :(得分:0)

您将onRequestPermissionsResult作为类的替代方法,而不是onCreate方法的局部功能。因此,只需在开始onCreate之前关闭onRequestPermissionsResult方法:

class MainActivity : AppCompatActivity()  {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
        val texto = TextView(this)
        val ID_REQUISICAO_ACCESS_FINE = 1
        setContentView(texto)
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
        when (requestCode) {
           ID_REQUISICAO_ACCESS_FINE -> {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // permission was granted, yay! Do the location-related task you need to do.
                    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                        Toast.makeText(this, "permission granted", Toast.LENGTH_LONG).show()
                    } else {
                        // permission denied, boo! Disable the functionality that depends on this permission.
                        Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show()
                    }
                    return
                }
            }
        }

    }
}