Intent call action doesn't work on Marshmallow

11

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?

android
android-intent
android-permissions
asked on Stack Overflow May 2, 2016 by Mauricio Sartori • edited Dec 26, 2019 by Cœur

3 Answers

21

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;
        }
    }
answered on Stack Overflow Jul 29, 2016 by Philip BH
8

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.

answered on Stack Overflow May 2, 2016 by KevinOrr
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.

answered on Stack Overflow May 2, 2016 by Etienne GT

User contributions licensed under CC BY-SA 3.0