yoon0506
yoon0506

Reputation: 33

android studio request permissions error (kotlin)

i tried to set request 5 permissions check in my code

Of course I added all five to the manifest. and i also tried to deleted the app and reinstalled it.

but it doesnt ask permissions at all

this is my root class

 override fun onCreate(savedInstanceState: Bundle?) {

    (...)

       if (checkAndRequestPermissions()) {

            Handler().postDelayed({

                val intent = Intent(this, ActivityMain::class.java)
                startActivity(intent)

            }, SPLASH_TIME_OUT.toLong())
        }

and checkAndRequestPermissions() method

 fun checkAndRequestPermissions(): Boolean {

        val internetP = ContextCompat.checkSelfPermission(this, Manifest.permission.INTERNET)
        val networkP = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_NETWORK_STATE)
        val wifiP = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_WIFI_STATE)
        val changeWifiP = ContextCompat.checkSelfPermission(this, Manifest.permission.CHANGE_WIFI_STATE)
        val notifiacationP = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_NOTIFICATION_POLICY)


        val listPermissionsNeeded = ArrayList<String>()

        if (internetP != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.INTERNET)
        }
        if (networkP != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.ACCESS_NETWORK_STATE)
        }
        if (wifiP != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.ACCESS_WIFI_STATE)
        }
        if (changeWifiP != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.CHANGE_WIFI_STATE)
        }
        if (notifiacationP != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.ACCESS_NOTIFICATION_POLICY)
        }
        if (!listPermissionsNeeded.isEmpty()) {
            ActivityCompat.requestPermissions(this, listPermissionsNeeded.toTypedArray(), REQUEST_ID_MULTIPLE_PERMISSIONS)
            return false
        }
        return true
    }

 if (checkAndRequestPermissions()) {

'checkAndRequstPermissions() ' value is always true

I think there's a problem here.

 if (!listPermissionsNeeded.isEmpty()) { 

even if listPersmissionsNeeded value is '0' it keep returning true, never going inside

I referred to this site ->

https://demonuts.com/kotlin-runtime-permissions/

Upvotes: 0

Views: 3060

Answers (2)

Sasi Kumar
Sasi Kumar

Reputation: 13288

Its seems all permission are already given.Its not problem in the code.

Because these are Normal permissions so no need to handle run time checker.

dangerous permissions and permission groups only required runtime permissions handlers.

Upvotes: 0

Shashank Kapsime
Shashank Kapsime

Reputation: 799

All the above mentioned permissions do not require runtime permission check and approval. These permissions are only to be mentioned in the Manifest file.

That's why function checkAndRequestPermissions() is always returning true.

See this link

https://developer.android.com/guide/topics/permissions/overview.html#normal-dangerous

Hope this helps. Happy Coding :)

Upvotes: 1

Related Questions