获得电话通知权限的正确方法是什么

时间:2017-10-11 13:23:12

标签: kotlin phone-call runtime-permissions

如何使用Kotlin请求权限。

我正在尝试拨打电话功能

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

我在清单中添加了用户权限 但仍然有相同的 error

5 个答案:

答案 0 :(得分:2)

您需要先向清单添加权限

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

在清单中添加权限后,以下代码可以正常工作“Number_to_call”将是您需要替换的号码

 val call = Intent(Intent.ACTION_DIAL)
 call.setData(Uri.parse("tel:" +"Number_to_call"))
 startActivity(call)

答案 1 :(得分:0)

您需要请求运行时权限,因为Android 6.0某些权限要求您在安装时询问并在运行时再次询问。

按照说明here说明了如何在运行时请求权限。

答案 2 :(得分:0)

您需要添加运行时权限。 Download the source code from here

//点击布局功能:

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

//请求权限响应

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              

        }
    })
}

//调用意图的功能

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

谢谢!

答案 3 :(得分:0)

首先,您需要首先向manifest添加权限:

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

这段代码用于您的方法:

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

答案 4 :(得分:0)

这是呼叫电话运行时权限的完整代码

  

步骤1 :-在清单中添加权限

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

步骤2 :-在checkAndroidVersion()

中调用此方法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)
}
}