12

I'm trying to start a call intent action on a device who has Marshmallow as OS, Using the same steps as usual (This is working on versions below):

Add permission:

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

Open the intent:

Intent intent = new Intent(Intent.ACTION_CALL);
                    intent.setData(Uri.parse("tel:" + getString(R.string.connect_phone)));
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);

This is the log I'm getting:

FATAL EXCEPTION: main

Process: com.app.calling, PID: 4250 java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.CALL dat=tel:xxxxxxxxxxxxx flg=0x10000000 cmp=com.android.server.telecom/.components.UserCallActivity VirtualScreenParam=Params{mDisplayId=-1, null, mFlags=0x00000000)} } from ProcessRecord{1618b01 4250:com.app.calling/u0a234} (pid=4250, uid=10234) with revoked permission android.permission.CALL_PHONE at android.os.Parcel.readException(Parcel.java:1620) at android.os.Parcel.readException(Parcel.java:1573) at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3130) at android.app.Instrumentation.execStartActivity(Instrumentation.java:1540) at android.app.Activity.startActivityForResult(Activity.java:4283) at android.app.Activity.startActivityForResult(Activity.java:4230) at android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:849) at android.support.v4.app.FragmentActivity$HostCallbacks.onStartActivityFromFragment(FragmentActivity.java:907) at android.support.v4.app.Fragment.startActivity(Fragment.java:919) at com.app.calling.activity.fragment.ConnectFragment$2.onGroupClick(ConnectFragment.java:44) at android.widget.ExpandableListView.handleItemClick(ExpandableListView.java:676) at android.widget.ExpandableListView.performItemClick(ExpandableListView.java:654) at android.widget.AbsListView$PerformClick.run(AbsListView.java:3821) at android.widget.AbsListView$3.run(AbsListView.java:5841) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:158) at android.app.ActivityThread.main(ActivityThread.java:7224) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

Again, this process is working fine in the previous version (Lollipop and kitkat)unfortunately isn't on Marshmallow, does anybody know why or what I'm missing?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mauricio Sartori
  • 2,533
  • 6
  • 26
  • 28

3 Answers3

25

Method to make call

public void onCall() {
        int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE);

        if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(
                    this,
                    new String[]{Manifest.permission.CALL_PHONE},
                    "123");
        } else {
            startActivity(new Intent(Intent.ACTION_CALL).setData(Uri.parse("tel:12345678901")));
        }
    }

Check permission

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {

            case 123:
                if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
                    onCall();
                } else {
                    Log.d("TAG", "Call Permission Not Granted");
                }
                break;

            default:
                break;
        }
    }
Philip Herbert
  • 4,599
  • 2
  • 37
  • 40
  • Bro What is the use of Second code Check permission, already you asked permission in First code and Android Manifest then do we need that? – MadhanMohan Nov 27 '20 at 10:22
  • The first code snippet: check if the permission has been previously granted and if it is not granted, prompt user to grant that permission. The second code snippet: check if the permission has now been granted and if it is, run the method onCall(). – Philip Herbert Nov 30 '20 at 13:54
  • https://stackoverflow.com/questions/65088812/how-to-do-call-intent-with-firebase-in-android-check-out-my-code-and-screensho – MadhanMohan Dec 01 '20 at 10:36
  • Check that question bruhhh, I did like what you said but still nothing works ,can you please check it and tell me – MadhanMohan Dec 01 '20 at 10:37
9

Beginning in android 6.0 (API 23), dangerous permissions must be declared in the manifest AND you must explicitly request that permission from the user. According to this list, CALL_PHONE is considered a dangerous permission.

Every time you perform an operation that requires a dangerous permission, you must check if that permission has been granted by the user. If it has not, you must request that it be granted. See Requesting Permissions at Run Time on Android Developers.

KevinOrr
  • 1,663
  • 3
  • 13
  • 24
1

For Marshmallow version and above you need to ask the permission at runtime not only in the manifest file. Here is the documentation:

Requesting Permissions at Run Time

Hope it helps.

Etienne GT
  • 185
  • 8