Al Lelopath Al Lelopath - 24 days ago 6
Android Question

Use VOIP app instead of phone dialer

I have the code below to start an intent to dial a phone number.
If this code is run on a device that doesn't have a phone, it runs right through, not throwing exceptions which surprised me.
Be that is it may, I want to know how to code so that if there is not a phone, then it should try to find a VOIP app.
How to do this?

ImageButton btnPhone = (ImageButton) view.findViewById(R.id.phone_image);
btnPhone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + employee.getPhone()));
try {
startActivity(intent);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getActivity(), "Can't make phone call", Toast.LENGTH_SHORT).show();
} catch (Exception ex) {
Toast.makeText(getActivity(), "Can't make phone call", Toast.LENGTH_SHORT).show();
}
}
});


Edit: retracted, will make a separate question.

Answer

Try this way:

1) test android device for cellular radio module presence (from here):

static public int getSDKVersion() {
    Class<?> build_versionClass = null;
    try {
        build_versionClass = android.os.Build.VERSION.class;
    } catch (Exception e) {
    }

    int retval = -1;
    try {
        retval = (Integer) build_versionClass.getField("SDK_INT").get(build_versionClass);
    } catch (Exception e) {
    }

    if (retval == -1)
        retval = 3; //default 1.5

    return retval;
}

static public boolean hasTelephony(Context context)
{
    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    if (tm == null)
        return false;

    //devices below are phones only
    if (Utils.getSDKVersion() < 5)
        return true;

    PackageManager pm = context.getPackageManager();

    if (pm == null)
        return false;

    boolean retval = false;
    try
    {
        Class<?> [] parameters = new Class[1];
        parameters[0] = String.class;
        Method method = pm.getClass().getMethod("hasSystemFeature", parameters);
        Object [] parm = new Object[1];
        parm[0] = "android.hardware.telephony";
        Object retValue = method.invoke(pm, parm);
        if (retValue instanceof Boolean)
            retval = ((Boolean) retValue).booleanValue();
        else
            retval = false;
    }
    catch (Exception e)
    {
        retval = false;
    }

    return retval;
}

with

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

permission and

<uses-feature android:name="android.hardware.telephony" android:required="false" />

uses feature.

And then if device havn't "phone module" try to call by all possible means (from here):

public void call(String dialNumber) {
    try{
    Intent callIntent = new Intent("android.intent.action.CALL_PRIVILEGED");
    callIntent.setData(Uri.parse("tel:" + dialNumber));
    startActivity(callIntent);
    }
    catch (Exception e) {
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse("tel:" + dialNumber));
        startActivity(callIntent);
    }
}

Or

2) try to found installed VOIP manually (from here)

private boolean appInstalledOrNot(String uri) {
        PackageManager pm = getPackageManager();
        boolean app_installed;
        try {
            pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
            app_installed = true;
        }
        catch (PackageManager.NameNotFoundException e) {
            app_installed = false;
        }
        return app_installed;
    }

3) try to make call via founded in p.2 apps, for example via Skype (from here)

public static void skype(String number, Context ctx) {
        try {
            //Intent sky = new Intent("android.intent.action.CALL_PRIVILEGED");
            //the above line tries to create an intent for which the skype app doesn't supply public api

                Intent sky = new Intent("android.intent.action.VIEW");
            sky.setData(Uri.parse("skype:" + number));
            Log.d("UTILS", "tel:" + number);
            ctx.startActivity(sky);
        } catch (ActivityNotFoundException e) {
            Log.e("SKYPE CALL", "Skype failed", e);
        }

    }