android - 使用 VOIP 应用程序代替电话拨号器

标签 android android-intent

我有下面的代码来启动调用电话号码的 Intent 。 如果这段代码在没有手机的设备上运行,它会直接运行,不会抛出令我惊讶的异常。 可能是这样,我想知道如何编码,以便如果没有电话,那么它应该尝试找到 VOIP 应用程序。 如何做到这一点?

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();
        }
    }
});

编辑:撤回,将提出一个单独的问题。

最佳答案

试试这个方法:

1) 测试 Android 设备是否存在蜂窝 radio 模块(来自 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;
}

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

许可和

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

使用功能。

然后,如果设备没有“电话模块”,请尝试通过所有可能的方式进行调用(来自 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);
    }
}

或者

2) 尝试手动找到已安装的 VOIP(来自 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) 尝试通过 p.2 应用程序调用电话,例如通过 Skype(来自 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);
        }

    }

关于android - 使用 VOIP 应用程序代替电话拨号器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40592081/

相关文章:

java - 从应用程序打开系统设置的特定部分

java - 从 ListActivity 条目启动 Intent - Android

android - 如何使用待定 Intent 启动新 Activity

java - 创建包含单词的数组列表并将其与用户输入的消息进行比较

Android兼容库(Android支持)安装问题

android - 如何使用左下角的应用程序图标在 sdcard 上创建文件夹?

android - 通过 Intent 分享图片(Facebook 等)

android - 加速度计传感器速率可以信任吗?

android - Android Studio 3.0 中的 AndroidManifest.xml 问题

Android弹出箭头