3

In my app I need in some situation to launch a call programmatically.

It works most of the time, but I can see that sometime it doesn't, specially on 2.3.7 version. Maybe that specific permission removed and is illegal at that version? I can't find a clue about the origin of the exception.

I have the permission in the Manifest:

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

The java code:

private void launchCall(String number, int calltype) {
            Intent callIntent = new Intent();
            callIntent.setAction(Intent.ACTION_CALL);
            callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            callIntent.setData(Uri.parse("tel:"+ number));
            if (calltype == CALL_TYPE_VIDEO) {
                callIntent.setAction(ACTION_VIDEO_CALL);
                callIntent.putExtra("videocall", true);
            }
            context.startActivity(callIntent);
        }

The exception trace:

java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.CALL dat=tel:xxx-xxx-xxxx flg=0x10000000 cmp=com.android.phone/.OutgoingCallBroadcaster } from ProcessRecord{415f7158 4016:com.tawkon/10092} (pid=4016, uid=10092) requires android.permission.CALL_PHONE
at android.os.Parcel.readException(Parcel.java:1322)
at android.os.Parcel.readException(Parcel.java:1276)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1359)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1374)
at android.app.ContextImpl.startActivity(ContextImpl.java:640)
at android.content.ContextWrapper.startActivity(ContextWrapper.java:258)
at com.tawkon.Service.PrecallSuggestionManager.launchCall(PrecallSuggestionManager.java:305)
at com.tawkon.Service.PrecallSuggestionManager.access$12(PrecallSuggestionManager.java:291)
at com.tawkon.Service.PrecallSuggestionManager$6.run(PrecallSuggestionManager.java:215)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3703)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
at dalvik.system.NativeStart.main(Native Method)
Bush
  • 2,433
  • 5
  • 34
  • 57

3 Answers3

0

I had the same issue, I found out that the permission weren't located at the right place! Copy this to your manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.shalom.itai.automaticdailer" >
    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Itai.S.
  • 144
  • 13
0

The below solution worked properly fine for me.

AndroidManifest.xml

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.CALL_PHONE" />

activity_main.xml

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/phoneNumberEditTextId"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="make phone call"
    android:onClick="makePhoneCall"/>

MainActivity.java

 private static final int MY_PERMISSIONS_REQUEST_CALL_PHONE = 1;

 public void makePhoneCall(View view) {

        int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE);
        if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, MY_PERMISSIONS_REQUEST_CALL_PHONE);
        } else {
            callPhone();
        }

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_CALL_PHONE: {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    callPhone();
                }
            }
        }
    }

    private void callPhone() {
        EditText editText = (EditText) findViewById(R.id.phoneNumberEditTextId);
        Intent intent = new Intent(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:" + editText.getText().toString()));
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
            startActivity(intent);
        }
    }
Siddarth Kanted
  • 5,738
  • 1
  • 29
  • 20
0

You need to add Call permission to your application manifest in visual studio IDE its in Properties of your project . .Click here to see the image .

Yeahia2508
  • 7,526
  • 14
  • 42
  • 71