Android Studio 中未授予 Action_Call 权限

Posted

技术标签:

【中文标题】Android Studio 中未授予 Action_Call 权限【英文标题】:Action_Call permission not granted in Android Studio 【发布时间】:2020-08-22 21:55:25 【问题描述】:

我正在尝试制作一个应用程序,如果输入号码但未授予权限则可以直接拨打电话,因此不拨打电话......

我已在 AndriodManifest.xml 中请求权限

每次我输入一个数字时,如果 Granted 是 Not Granted,就会弹出“Hello”。 我的代码:

MainActivity.java

package com.example.block9;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity 

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    

    public void redirectmessage(View v) 
        String number = (((EditText) findViewById(R.id.mobnumber)).getText()).toString();
        String message = (((EditText) findViewById(R.id.textmessage)).getText()).toString();
        Uri num = Uri.parse("smsto:" + number);
        Intent smsIntent = new Intent(Intent.ACTION_SENDTO, num);
        smsIntent.putExtra("sms_body", message);
        startActivity(smsIntent);
    

    public void redirectcall(View v) 
        String number = (((EditText) findViewById(R.id.mobnumber)).getText()).toString();
        Toast t = null;
        //t.makeText(getApplicationContext(),number,Toast.LENGTH_SHORT).show();
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse("tel:" + number));
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) 
            t.makeText(getApplicationContext(),"Hello",Toast.LENGTH_SHORT).show();
            return;
        
        startActivity(callIntent);
    

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.block9">

    <uses-permission android:name="android.permission.CALL_PHONE"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

任何帮助将不胜感激!!`在此处输入代码。

【问题讨论】:

【参考方案1】:

如果您的目标设备API is &lt;22,那么您不需要请求许可,因为许可会自动提供但是作为开发人员,您必须为每一种可能性做好准备。如果目标设备API is &gt;=23 则您必须手动请求权限。更多信息请阅读this

public void redirectcall(View v) 
            String number = (((EditText) findViewById(R.id.mobnumber)).getText()).toString();
            Toast t = null;
            //t.makeText(getApplicationContext(),number,Toast.LENGTH_SHORT).show();
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:" + number));
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) 
                t.makeText(getApplicationContext(),"Hello",Toast.LENGTH_SHORT).show();

                grantPermission();

                return;
            
            startActivity(callIntent);
        

    private void grantPermission() 

          // show your dialog box to ask for permission
            new AlertDialog.Builder(this)
                    .setTitle("Call Permission Required")
                    .setMessage("This App needs Call permission, to function properly")
                    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
                        @Override
                        public void onClick(DialogInterface dialog, int which) 
                            Toast.makeText(MainActivity.this, "Permission Denied", Toast.LENGTH_SHORT)
                                    .show();
                        
                    )
                    .setPositiveButton("Ok", new DialogInterface.OnClickListener() 
                        @Override
                        public void onClick(DialogInterface dialog, int which) 
                            //here permission will be given
                            ActivityCompat.requestPermissions(MainActivity.this, new String[]Manifest.permission.CALL_PHONE, 3); // 3 is requestCode and can be any number
                        
                    )
                    .create()
                    .show();
    

现在发出请求后,我们会调用@Override 方法onRequestPermissionsResult() 来处理Rejected 或accepted Request 的场景

*@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) 
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode)
            case 3: //remember that 3 is the same number which we specified while requesting
            
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) 

                    // permission was granted, yay! Do the
                    // Call-related task you need to do.
                    if (ContextCompat.checkSelfPermission(this,
                            Manifest.permission.CALL_PHONE)
                            == PackageManager.PERMISSION_GRANTED) 
                        redirectcall();
                    


                else
                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                    Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show();
                

            

        
    *

完整的 MainActivity.java 代码在这里:https://gist.github.com/Shoaibpython/eda5394ee4bc441396d68d5ef603cd3

如果您发现任何错误,请考虑在此处回复。

【讨论】:

添加后调用工作,向我的代码授予权限功能 在我的代码中添加grantPermission() 后它起作用了,但我没有得到onRequestPermissionsResult() 的意义,而且它给出了错误,因为它调用了需要参数的redirectcall()。

以上是关于Android Studio 中未授予 Action_Call 权限的主要内容,如果未能解决你的问题,请参考以下文章

android studio中未使用的导入

Android Studio中未生成MainActivityBinding文件

使用新的 M1 兼容模拟器在 Android Studio 中未检测到 Android 模拟器

Recycler视图在android studio中未显示一张卡片视图

Android Studio:此环境中未提供编译器。也许您在 JRE 而不是 JDK 上运行? [复制]

在模块中未找到Android Facet