Starting Viber Call Request from my android app

962 views Asked by At

I am listing all the contacts from the phone and call a viber intent opening viber app directly from my android app.I found some example online but all are too old may be couple of years before and none of them have worked for me.

Uri uri = getUriFromPhoneNumber(phoneNumber, context);
    if (uri != null) {

        Intent localIntent4 = new Intent("com.viber.voip.action.CALL");
        localIntent4.setType("vnd.android.cursor.item/vnd.com.viber.voip.call");
        localIntent4.setData(Uri.parse("tel:" + uri));
        startActivity(localIntent4);

        }else {
        Toast.makeText(context, "Number is not in Viber Contacts List", Toast.LENGTH_LONG).show();
    }

I have included read_contacts permission in my androidmanifest.xml file. But upon this always throws security exception.One solution i was recommended was to include

activity android:name="com.ujjwalmainali.univhub.TestActivity"
        android:launchMode="singleInstance"
        android:exported="true"
        android:screenOrientation="portrait">

android:exported="true" but this didn't work for me.What is wrong here.What kind of permission do i need to make intent call.

Here is the logcat of the exception

java.lang.SecurityException: Permission Denial: starting Intent { act=com.viber.voip.action.CALL dat=tel:xxxxxxxxxxxxx.xxxxxxx.xxxxxxxxxxxxxxxxx cmp=com.viber.voip/.phone.PhoneFragmentActivity } from ProcessRecord{271db4b9 3911:com.ujjwalmainali.univhub/u0a219} (pid=3911, uid=10219) not exported from uid 10204
    at android.os.Parcel.readException(Parcel.java:1549)
    at android.os.Parcel.readException(Parcel.java:1502)
    at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2463)
    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1619)
    at android.app.Activity.startActivityForResult(Activity.java:3761)
    at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
    at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:77)
    at android.app.Activity.startActivityForResult(Activity.java:3722)
    at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
    at android.app.Activity.startActivity(Activity.java:4032)
    at android.app.Activity.startActivity(Activity.java:4000)
    at com.ujjwalmainali.univhub.TestActivity.callToViberContact(TestActivity.java:57)
    at com.ujjwalmainali.univhub.TestActivity$1.onClick(TestActivity.java:33)
    at android.view.View.performClick(View.java:4785)
    at android.view.View$PerformClick.run(View.java:19869)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:155)
    at android.app.ActivityThread.main(ActivityThread.java:5696)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
1

There are 1 answers

1
mhdjazmati On

add this tag to your manifest :

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

and for runTimePermission :

if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                        ActivityCompat.requestPermissions(activityCompat, new String[]{Manifest.permission.CALL_PHONE}, 123);
                        return;
                    }
                    context.startActivity(localIntent4);

. . . .

public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                       int[] grantResults) {
    for (int grantResult : grantResults) {
        if (grantResult == PackageManager.PERMISSION_DENIED) {
            return;
        }
    }
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    startActivity(localIntent4 );
}

localIntent4 is the intent that you are using .

EDIT :

also you need to set this flag to your intent

            localIntent4.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);