运行时权限请求不起作用

时间:2017-11-14 19:21:08

标签: android kotlin

根据我对this example的理解,我正在使用Kotlin的运行时权限尝试我的第一个Android应用,但是权限请求对话框永远不会出现。

在上述参考示例中编写Kotlin等效Java代码时是否有任何错误?

应用程序假定在单击按钮时调用预定义的数字。

string.xml

<resources>
    <string name="app_name">Kotlin</string>
    <string name="title_activity_display">Call</string>
</resources>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.home.kotlin.MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Display" />

</LinearLayout>

MainActivity.kt

package com.example.home.kotlin

import android.Manifest
import android.app.AlertDialog
import android.content.DialogInterface
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.Toast

class MainActivity : AppCompatActivity() {

    private var mCall: Button? = null
    private var callIntent: Intent? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        mCall = findViewById(R.id.button)

        mCall!!.setOnClickListener { _: View ->
            callIntent = Intent(Intent.ACTION_CALL)
            callIntent!!.data = Uri.parse("tel:0123456789")

            val hasCallPhone = checkSelfPermission(Manifest.permission.CALL_PHONE)
            if (hasCallPhone != PackageManager.PERMISSION_GRANTED) {
                if (!shouldShowRequestPermissionRationale(Manifest.permission.CALL_PHONE)) {
                    showMessageOKCancel("You need to allow access to dialer",
                    DialogInterface.OnClickListener { _, _ ->
                        requestPermissions(arrayOf(Manifest.permission.CALL_PHONE),
                                1)
                    })
                    return@setOnClickListener
                }
                requestPermissions(arrayOf(Manifest.permission.CALL_PHONE),
                        1)
                return@setOnClickListener
            }
        }
    }

    private fun showMessageOKCancel(message: String, okListener: DialogInterface.OnClickListener) {
        AlertDialog.Builder(this@MainActivity)
                .setMessage(message)
                .setPositiveButton("OK", okListener)
                .setNegativeButton("Cancel", null)
                .create()
                .show()
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
        when (requestCode) {
            1 -> if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission Granted
                Toast.makeText(this@MainActivity, "Permission Granted", Toast.LENGTH_SHORT)
                        .show()
                startActivity(callIntent)
                this.finish()
            } else {
                // Permission Denied
                Toast.makeText(this@MainActivity, "Permission Denied", Toast.LENGTH_SHORT)
                        .show()
            }
            else -> super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        }
    }

}

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:2)

您还需要将权限添加到manifest.xml。

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