Search code examples
androidphone-call

Android phone call not working


I just want to perform phone calls from my application.I tried everything else.

I tired all the solutions from internet including mkyong,tutorialspoint and many more.

phoneCalls.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View arg0) {
                            Intent callIntent = new Intent(Intent.ACTION_CALL);
                            callIntent.setData(Uri.parse("tel:0377778888"));

                            if (ActivityCompat.checkSelfPermission(getActivity(),
                                    Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                                return;
                            }
                            startActivity(callIntent);
                        }
                    });

and inside my manifest I asked permission as:

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

The above code doesnot shows any error as well.I again tried this one

button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:0377778888"));
                startActivity(callIntent);

            }

        });

    }

On trying this code it shows following error at logcat:

java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.CALL dat=tel:xxxxxxxxxx cmp=com.android.server.telecom/.components.UserCallActivity } from ProcessRecord{80f03c 26994:com.example.user.home/u0a73} (pid=26994, uid=10073) 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:2680)
                                                                           at android.app.Instrumentation.execStartActivity(Instrumentation.java:1509)
                                                                           at android.app.Activity.startActivityForResult(Activity.java:3930)
                                                                           at android.app.Activity.startActivityForResult(Activity.java:3890)
                                                                           at android.app.Activity.startActivity(Activity.java:4213)
                                                                           at android.app.Activity.startActivity(Activity.java:4181)
                                                                           at com.example.user.home.Main3Activity$1.onClick(Main3Activity.java:30)
                                                                           at android.view.View.performClick(View.java:5204)
                                                                           at android.view.View$PerformClick.run(View.java:21158)
                                                                           at android.os.Handler.handleCallback(Handler.java:739)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                           at android.os.Looper.loop(Looper.java:148)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5461)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

With hope I am leaving this question here.Someone please help


Solution

  • Because your are trying it on Marshmallow 6.0 and you didn't set Runtime permission for Marshmallow or above android version.

    Your code will run in below 6.0 but not in 6.0 or above.

    Replcae your code by this.

    button.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View arg0)
            {
                callPhoneNumber();
            }
        });
    }
    

    Create this Call Method with permission check

    public void callPhoneNumber()
    {
        try
        {
            if(Build.VERSION.SDK_INT > 22)
            {
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
    
                    ActivityCompat.requestPermissions(TestActivity.this, new String[]{Manifest.permission.CALL_PHONE}, 101);
    
                    return;
                }
    
                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:" + "0377778888"));
                startActivity(callIntent);
    
            }
            else {
                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:" + "0377778888"));
                startActivity(callIntent);
            }
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }
    

    When user granted the permission the control comes here

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                              int[] grantResults)
    {
        if(requestCode == 101)
        {
            if(grantResults[0] == PackageManager.PERMISSION_GRANTED)
            {
                callPhoneNumber();
            }
            else
            {
                Log.e(TAG, "Permission not Granted");
            }
        }
    }