0

I need to call a given phonenumber from my application.

so i have to verify the permission for call.

but the dialog is showing in my application, straightly it is going the deny function. what is the issue i have done here. please review my code and give me a solution for this.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, 105);
            } else {
                startActivity(callIntent);
            }
        } else {
            startActivity(callIntent);
        }

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 105:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                callJobseeker();
            } else {
                Toast.makeText(getApplicationContext(), "You cannot make a call.Because call permission is denied", Toast.LENGTH_SHORT).show();
            }
            break;
                 }
                }

in the manifest file am given the permission like :

<uses-permission android:name="android.permission.CALL_PHONE" />
<permission android:name="com.example.permission.CALL_PHONE"
    />
Krishna Veni
  • 2,217
  • 8
  • 27
  • 53

4 Answers4

0

try with shouldShowRequestPermissionRationale

//Function use for request permission to access camera & gallary.
    private void requestPermission() {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)) {
            Toast.show(context, getString(R.string.requestPermission));
        } else {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, PERMISSION_REQUEST_CODE);
        }
    }
Vishal Patoliya ツ
  • 3,170
  • 4
  • 24
  • 45
0

You can do like this. make class like below

public class CheckPermisson  {

    private Context context;

    public CheckPermisson(Context context) {
        this.context = context;
    }

    public boolean checkCallPermission() {
        if (ContextCompat.checkSelfPermission(context.getApplicationContext(), Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
            Log.i("TAG","Call Permission is granted");
          return true;


        } else {

            return false;
        }

    }
    public boolean checkExternalStoragePermission() {
        if (ContextCompat.checkSelfPermission(context.getApplicationContext(),android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED) {
            Log.i("TAG", "Storage Permission is granted");
            return true;
        }
        else
        {
            return false;
        }
    }
    public boolean checkLocationPermission() {
        if (ContextCompat.checkSelfPermission(context.getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(context.getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            Log.i("TAG", "Location Permission is granted");
            return true;
        }
        else
        {
            return false;
        }
    }
}

You can use like below

CheckPermisson permisson = new CheckPermisson(getActivity());

if (Build.VERSION.SDK_INT >= 23) {
    if (permisson.checkCallPermission()) {
        itemSelectedPosition = position;
        createDialogBox(position);
    } else {
        requestPermissions(new String[]{Manifest.permission.CALL_PHONE}, CALL_PERMISSION_REQUEST_CODE);
    }
} else {
    createDialogBox(position);
}

add below permission in manifest

<uses-permission android:name="android.permission.CALL_PHONE" />
V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
Vishal Thakkar
  • 2,117
  • 2
  • 16
  • 33
0

You should be checking if the user has granted permission of CALL_PHONE by using:

if (checkSelfPermission(android.Manifest.permission. CALL_PHONE)
            == PackageManager.PERMISSION_GRANTED) {
        Log.v(TAG,"Permission is granted");
        return true; 
}
Burak iren
  • 338
  • 3
  • 14
0

I got the answer.,

My mistake in the post is.,

In the android manifest file, i have removed this line.,

<permission android:name="com.example.permission.CALL_PHONE"
    />

i have changed the following code.,

if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.CALL_PHONE}, 105);
                } else {
                    startActivity(callIntent);
                }
Krishna Veni
  • 2,217
  • 8
  • 27
  • 53