1

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

我已在 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>

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

4

1 回答 1

0

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

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()来处理拒绝或接受请求的场景

*@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

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

于 2020-05-07T16:41:55.443 回答