Ionic 插件 inappbrowser 内的 H5 怎么打开安卓 app

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a727911438/article/details/81588669

一、如果项目有 run 或者 build 过:

1、打开 /platforms/android/src/org/apache/cordova/inappbrowser/InAppBrowser.java

2、找到 shouldOverrideUrlLoading 函数,替换为:


@Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
    if (url.startsWith(WebView.SCHEME_TEL)) {
        try {
            Intent intent = new Intent(Intent.ACTION_DIAL);
            intent.setData(Uri.parse(url));
            cordova.getActivity().startActivity(intent);
            return true;
        } catch (android.content.ActivityNotFoundException e) {
            LOG.e(LOG_TAG, "Error dialing " + url + ": " + e.toString());
        }
    }
    // 去掉 if 中的 intent 
    else if (url.startsWith("geo:") || url.startsWith(WebView.SCHEME_MAILTO) || url.startsWith("market:")) {
        try {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(url));
            cordova.getActivity().startActivity(intent);
            return true;
        } catch (android.content.ActivityNotFoundException e) {
            LOG.e(LOG_TAG, "Error with " + url + ": " + e.toString());
        }
    }
    else if (url.startsWith("sms:")) {
        try {
            Intent intent = new Intent(Intent.ACTION_VIEW);

            // Get address
            String address = null;
            int parmIndex = url.indexOf('?');
            if (parmIndex == -1) {
                address = url.substring(4);
            } else {
                address = url.substring(4, parmIndex);

                // If body, then set sms body
                Uri uri = Uri.parse(url);
                String query = uri.getQuery();
                if (query != null) {
                    if (query.startsWith("body=")) {
                        intent.putExtra("sms_body", query.substring(5));
                    }
                }
            }
            intent.setData(Uri.parse("sms:" + address));
            intent.putExtra("address", address);
            intent.setType("vnd.android-dir/mms-sms");
            cordova.getActivity().startActivity(intent);
            return true;
        } catch (android.content.ActivityNotFoundException e) {
            LOG.e(LOG_TAG, "Error sending sms " + url + ":" + e.toString());
        }
    }
    else {    // 关键是这里,直接原生浏览器解析,这样就能打开各种各样的 scheme 了
        try {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(url));
            cordova.getActivity().startActivity(intent);
            return true;
        } catch (android.content.ActivityNotFoundException e) {
            LOG.e(LOG_TAG, "Error with " + url + ": " + e.toString());
        }
    }
    return false;
}

然后再 run 或 build 一遍即可。

二、如果没有 run 或 build 过

1、打开 /plugins/cordova-plugin-inappbrowser/src/android/InAppBrowser.java

2、将 shouldOverrideUrlLoading 函数替换为以上代码即可。

猜你喜欢

转载自blog.csdn.net/a727911438/article/details/81588669