-1

How to request permissions using Kotlin.

I am trying to make a phone call function

fun buChargeEvent(view: View){
    var number: Int = txtCharge.text.toString().toInt()
    val intentChrage = Intent(Intent.ACTION_CALL)
    intent.data = Uri.parse("tel:$number")
    startActivity(intentChrage)
}

I added user permissions in manifest but still having the same error .

Quick learner
  • 10,632
  • 4
  • 45
  • 55
  • 1
    Maybe this helps: https://developer.android.com/training/permissions/requesting.html?hl=en-419 – Héctor Oct 11 '17 at 13:24
  • 2
    Possible duplicate of [android request runtime permission to call action](https://stackoverflow.com/questions/42057040/android-request-runtime-permission-to-call-action) – jrtapsell Oct 11 '17 at 13:35
  • why only tag kotlin? at last add android. And your question is duplicate [request permission](https://stackoverflow.com/questions/42057040/android-request-runtime-permission-to-call-action) – kurt Oct 12 '17 at 08:41

5 Answers5

3

You need to add permission to your manifest first

<uses-permission android:name="android.permission.CALL_PHONE" />

After permission added in manifest following code would work fine for you "Number_to_call" will be youe number that is need to be replaced

 val call = Intent(Intent.ACTION_DIAL)
 call.setData(Uri.parse("tel:" +"Number_to_call"))
 startActivity(call)
arjun shrestha
  • 1,379
  • 1
  • 10
  • 13
1

You need to add the run time permission. Download the source code from here

//Click function of layout:

  rl_call.setOnClickListener {
        if (boolean_call) {
            phonecall()
        }else {
            fn_permission(Manifest.permission.CALL_PHONE,CALLMODE)
        }
    }

// Request permission response

fun fn_permission(permission:String,mode:Int){
    requestPermissions(permission, object : PermissionCallBack {
        override fun permissionGranted() {
            super.permissionGranted()
            Log.v("Call permissions", "Granted")
                boolean_call=true              
                phonecall()        

        }

        override fun permissionDenied() {
            super.permissionDenied()
            Log.v("Call permissions", "Denied")
                boolean_call=false              

        }
    })
}

// function to call intent

   fun phonecall() {
    val intent = Intent(Intent.ACTION_CALL);
    intent.data = Uri.parse("tel:1234567890s")
    startActivity(intent)
}

Thanks!

Deepshikha Puri
  • 2,104
  • 22
  • 23
1

First you need to add permission to your manifest first :

<uses-permission android:name="android.permission.CALL_PHONE" />

This bit of code is used on the place of your method :

fun buChargeEvent(view: View) {
    var number: Int = txtCharge.text.toString().toInt()
    val callIntent = Intent(Intent.ACTION_CALL)
    callIntent.data = Uri.parse("tel:$number")
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this as Activity,
                Manifest.permission.CALL_PHONE)) {
        } else {
            ActivityCompat.requestPermissions(this,
                    arrayOf(Manifest.permission.CALL_PHONE),
                    MY_PERMISSIONS_REQUEST_CALL_PHONE)
        }
    }
    startActivity(callIntent)
}
Nomade
  • 1,760
  • 2
  • 18
  • 33
0

You need to request the runtime permission, since Android 6.0 certain permissions require you to ask at install and again at runtime.

Following the instructions here explains how to ask for permission at runtime.

jrtapsell
  • 6,719
  • 1
  • 26
  • 49
0

This is the complete code for runtime permissions for Call Phone

Step 1:- add permission in manifest

<uses-permission android:name="android.permission.CALL_PHONE" />

Step 2:- Call this method checkAndroidVersion() in onCreate()

fun checkAndroidVersion() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (checkAndRequestPermissions()) {
            } else {

            }

        } else {
            // do code for pre-lollipop devices
        }

    }

val REQUEST_ID_MULTIPLE_PERMISSIONS = 1



fun checkAndRequestPermissions(): Boolean {
        val call = ContextCompat.checkSelfPermission(this@MainActivity, Manifest.permission.CALL_PHONE)
        val listPermissionsNeeded = ArrayList<String>()
        if (call != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.CALL_PHONE)
        }

        if (!listPermissionsNeeded.isEmpty()) {
            ActivityCompat.requestPermissions(this@MainActivity, listPermissionsNeeded.toTypedArray(), REQUEST_ID_MULTIPLE_PERMISSIONS)
            return false
        }
        return true
    }



fun checkAndRequestPermissions(): Boolean {
        val call = ContextCompat.checkSelfPermission(this@MainActivity, Manifest.permission.CALL_PHONE)
        val listPermissionsNeeded = ArrayList<String>()
        if (call != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.CALL_PHONE)
        }

        if (!listPermissionsNeeded.isEmpty()) {
            ActivityCompat.requestPermissions(this@MainActivity, listPermissionsNeeded.toTypedArray(), REQUEST_ID_MULTIPLE_PERMISSIONS)
            return false
        }
        return true
    }



override fun onRequestPermissionsResult(requestCode: Int,
                                            permissions: Array<String>, grantResults: IntArray) {
        Log.d("in fragment on request", "Permission callback called-------")
        when (requestCode) {
            REQUEST_ID_MULTIPLE_PERMISSIONS -> {

                val perms = HashMap<String, Int>()
                // Initialize the map with both permissions
                perms[Manifest.permission.CALL_PHONE] = PackageManager.PERMISSION_GRANTED
                // Fill with actual results from user
                if (grantResults.size > 0) {
                    for (i in permissions.indices)
                        perms[permissions[i]] = grantResults[i]
                    // Check for both permissions
                    if (perms[Manifest.permission.CALL_PHONE] == PackageManager.PERMISSION_GRANTED
                            ) {
                        print("Storage permissions are required")
                        // process the normal flow
                        //else any one or both the permissions are not granted
                    } else {
                        Log.d("in fragment on request", "Some permissions are not granted ask again ")
                        //permission is denied (this is the first time, when "never ask again" is not checked) so ask again explaining the usage of permission
                        //                        // shouldShowRequestPermissionRationale will return true
                        //show the dialog or snackbar saying its necessary and try again otherwise proceed with setup.
                        if (ActivityCompat.shouldShowRequestPermissionRationale(this@MainActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE) || ActivityCompat.shouldShowRequestPermissionRationale(this@MainActivity, Manifest.permission.CAMERA) || ActivityCompat.shouldShowRequestPermissionRationale(this@MainActivity, Manifest.permission.READ_EXTERNAL_STORAGE) || ActivityCompat.shouldShowRequestPermissionRationale(this@MainActivity, Manifest.permission.ACCESS_FINE_LOCATION)) {
                            showDialogOK("Call  permission is required for this app",
                                    DialogInterface.OnClickListener { dialog, which ->
                                        when (which) {
                                            DialogInterface.BUTTON_POSITIVE -> checkAndRequestPermissions()
                                            DialogInterface.BUTTON_NEGATIVE -> {
                                            }
                                        }// proceed with logic by disabling the related features or quit the app.
                                    })
                        } else {
                            Toast.makeText(this@MainActivity, "Go to settings and enable permissions", Toast.LENGTH_LONG)
                                    .show()
                            //                            //proceed with logic by disabling the related features or quit the app.
                        }//permission is denied (and never ask again is  checked)
                        //shouldShowRequestPermissionRationale will return false
                    }
                }
            }
        }

    }

    fun showDialogOK(message: String, okListener: DialogInterface.OnClickListener) {
        AlertDialog.Builder(this@MainActivity)
                .setMessage(message)
                .setPositiveButton("OK", okListener)
                .setNegativeButton("Cancel", okListener)
                .create()
                .show()
    }
**Step 3**:- On button click
fun buChargeEvent(view: View){
if(checkAndRequestPermissions(){
    var number: Int = txtCharge.text.toString().toInt()
    val intentChrage = Intent(Intent.ACTION_CALL)
    intent.data = Uri.parse("tel:$number")
    startActivity(intentChrage)
}
}
Quick learner
  • 10,632
  • 4
  • 45
  • 55