java.lang.Securityexception: Permission Denial: Tarting Intent

Asked

Viewed 1,542 times

1

I’m developing an Android phone call app. Give me the following exception:

java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.CALL dat=tel:xxxxxxxxx cmp=com.android.server.telecom/.components.UserCallActivity } from ProcessRecord{1a0df5b 9112:pt.ipp.estg.broadcastapp/u0a265} (pid=9112, uid=10265) with revoked permission android.permission.CALL_PHONE

This occurs when you enter if and pass the Startactivity line of the following code block:

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

        edtNumTel = findViewById(R.id.edt_numero_telefone);
        btnFazerChamada = findViewById(R.id.btn_fazer_chamada);
        btnFazerChamada.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               Intent it = new Intent(Intent.ACTION_CALL);
               it.setData(Uri.parse("tel:"+edtNumTel.getText().toString()));
               if (ActivityCompat.checkSelfPermission( MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    startActivity(it);
                 return;
                }
            }
        });

I added the permission to Androidmanifest.xml:

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

And the following code block on Activity Main:

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch(requestCode) {
        case 1:{
            if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(getApplicationContext(), "Permissao aceite", Toast.LENGTH_SHORT).show();

            }
        }
    }
}

1 answer

1

It turns out that the if is on the following condition: "If the app is not allowed to make calls, then make a call."

if (ActivityCompat.checkSelfPermission( MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
    startActivity(it);
    return;
}

Change the above section to:

if (ActivityCompat.checkSelfPermission( MainActivity.this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
    startActivity(it);
}
else {
    /* Exibe a tela para o usuário dar a permissão. */
    ActivityCompat.requestPermissions(
        NomeDeSuaActivity.this,
        new String[]{Manifest.permission.CALL_PHONE},
        REQUEST_CODE);
}

Replace the method onRequestPermissionsResult thus:

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch(requestCode) {
        case REQUEST_CODE:{
            if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(getApplicationContext(), "Permissao aceite", Toast.LENGTH_SHORT).show();

            }
        }
    }
}

Ps.: Create the verified REQUEST_CODE. That way private static final Integer REQUEST_CODE = 1

  • Do not recognize me the REQUEST_CODE

  • I think REQUEST_CODE is a variable that is initialized somewhere in Activity.

  • Yes, you must create an integer type variable and store a code in it. Magic Numbers.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.