【问题标题】:What's the right way to get permissions for phone call intent获得电话呼叫意图权限的正确方法是什么
【发布时间】:2017-10-11 13:23:11
【问题描述】:

如何使用 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 .

【问题讨论】:

标签: kotlin phone-call runtime-permissions


【解决方案1】:

您需要先为清单添加权限

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

【讨论】:

    【解决方案2】:

    您需要添加运行时权限。 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】:

      首先,您需要先为您的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】:

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

        按照说明 here 解释如何在运行时请求许可。

        【讨论】:

          【解决方案5】:

          这是 Call Phone 运行时权限的完整代码

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

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

          第 2 步:- 在onCreate() 中调用此方法checkAndroidVersion()

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

          【讨论】:

            猜你喜欢
            • 2017-12-23
            • 1970-01-01
            • 2010-10-10
            • 2012-03-14
            • 2011-03-29
            • 2016-01-11
            • 2013-02-11
            • 2010-09-10
            相关资源
            最近更新 更多