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();
}
}
}
}
Do not recognize me the REQUEST_CODE
– Eusébio Dobrões Dobrões
I think REQUEST_CODE is a variable that is initialized somewhere in Activity.
– ZelDias
Yes, you must create an integer type variable and store a code in it. Magic Numbers.
– Valdeir Psr