【问题标题】:When I overriding function onRequestPermissionsResult is not working当我覆盖函数 onRequestPermissionsResult 不起作用时
【发布时间】:2019-04-12 19:29:07
【问题描述】:

当我覆盖函数 onRequestPermissionsResult 不起作用时。

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

错误信息是

修饰符“覆盖”不适用于“局部函数”

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

            }
        }

【问题讨论】:

    标签: android kotlin permissions


    【解决方案1】:

    你有 onRequestPermissionsResult 作为类的重写方法,而不是 onCreate 方法的本地函数。所以,在启动onRequestPermissionsResult之前关闭onCreate方法:

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

    【讨论】:

    • 非常感谢。是工作。我需要更多地了解 android 类...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-21
    • 1970-01-01
    • 2011-06-02
    • 1970-01-01
    • 2019-09-25
    相关资源
    最近更新 更多