Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Handling Marshmallow

Kevin Perez
PLUS
Kevin Perez
Courses Plus Student 8,180 Points

Call requires permissions that may be rejected by user

I'm still having an error when I write this line of code: startActivity(intent);

It says: "Call requires permission which may be rejected by user: code should explicitly check to see if permission is available (with checkPermission) or explicitly handle a potential SecurityException"

This is my code: [CODE]

private static final int PERMISSIONS_REQUEST_CODE = 11;

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

    Button callButton = (Button) findViewById(R.id.callButton);
    callButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if(checkSelfPermission(Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_DENIED){
                    if(shouldShowRequestPermissionRationale(Manifest.permission.CALL_PHONE)){
                        new AlertDialog.Builder(MainActivity.this)
                                .setTitle("Call Permission")
                                .setMessage("Hi there! We can't call anyone without the call permission, could you please grant it?")
                                .setPositiveButton("Yep", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        requestPermissions(new String[]{Manifest.permission.CALL_PHONE}, PERMISSIONS_REQUEST_CODE);
                                    }
                                })
                                .setNegativeButton("No thanks", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        Toast.makeText(MainActivity.this, ":(", Toast.LENGTH_SHORT).show();
                                    }
                                }).show();
                    } else
                        requestPermissions(new String[]{Manifest.permission.CALL_PHONE}, PERMISSIONS_REQUEST_CODE);
                } else 
                    makeCall();
            } else
                makeCall();
        }
    });
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if(requestCode == PERMISSIONS_REQUEST_CODE){
        if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
            makeCall();
    }
}

private void makeCall() {
    Intent intent = new Intent(Intent.ACTION_CALL);
    intent.setData(Uri.parse("tel:" + "12345678"));
    startActivity(intent);
}

1 Answer

Mark VonGyer
Mark VonGyer
21,239 Points

The exception relates to the inbuilt security rights. The user will be prompted to allow access to whichever security module is required. If the user declines access, your code will blow up and you aren't catching an exception. .It is basically recognising your app may crash.