java - 如何直接在应用内浏览器(Chrome 自定义选项卡)中打开外部链接,而不提示选择浏览器

标签 java android android-studio android-webview

我需要直接在应用内浏览器(Chrome 自定义选项卡)中打开外部链接,而不会提示我在 Android Webview 应用中选择浏览器

现在,如果我点击应用程序中的外部链接,它会提示我并要求我选择任何浏览器(Chrome、Opera、Edge)。如果我点击 chrome,则链接会在应用程序内的 Chrome 自定义选项卡中打开,但我希望提示不应出现,链接应直接在 Chrome 自定义选项卡中打开(就像在 Instagram、FB 等上发生的情况一样)

/*--- actions based on URL structure ---*/

    public boolean url_actions(WebView view, String url){
        boolean a = true;
        // show toast error if not connected to the network
        if (!ASWP_OFFLINE && !DetectConnection.isInternetAvailable(MainActivity.this)) {
            Toast.makeText(getApplicationContext(), getString(R.string.check_connection), Toast.LENGTH_SHORT).show();

        // use this in a hyperlink to redirect back to default URL :: href="refresh:android"
        } else if (url.startsWith("refresh:")) {
            String ref_sch = (Uri.parse(url).toString()).replace("refresh:","");
            if(ref_sch.matches("URL")){
                CURR_URL = ASWV_URL;
            }
            pull_fresh();

        // use this in a hyperlink to launch default phone dialer for specific number :: href="tel:+919876543210"
        } else if (url.startsWith("tel:")) {
            Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
            startActivity(intent);

        // use this to open your apps page on google play store app :: href="rate:android"
        } else if (url.startsWith("rate:")) {
            final String app_package = getPackageName(); //requesting app package name from Context or Activity object
            try {
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + app_package)));
            } catch (ActivityNotFoundException anfe) {
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + app_package)));
            }

        // sharing content from your webview to external apps :: href="share:URL" and remember to place the URL you want to share after share:___
        } else if (url.startsWith("share:")) {
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_SUBJECT, view.getTitle());
            intent.putExtra(Intent.EXTRA_TEXT, view.getTitle()+"\nVisit: "+(Uri.parse(url).toString()).replace("share:",""));
            startActivity(Intent.createChooser(intent, getString(R.string.share_w_friends)));

        // use this in a hyperlink to exit your app :: href="exit:android"
        } else if (url.startsWith("exit:")) {
            aswm_exit();

        // getting location for offline files
        } else if (url.startsWith("offloc:")) {
            String offloc = ASWV_URL+"?loc="+get_location();
            aswm_view(offloc,false, asw_error_counter);
            Log.d("OFFLINE LOC REQ",offloc);

        // creating firebase notification for offline files
        } else if (url.startsWith("fcm:")) {
            String fcm = ASWV_URL+"?fcm="+fcm_token();
            aswm_view(fcm,false, asw_error_counter);
            Log.d("OFFLINE_FCM_TOKEN",fcm);

        // opening external URLs in android default web browser
        } else if (ASWP_EXTURL && !aswm_host(url).equals(ASWV_HOST) && !url.contains("oauth"))  {
            aswm_view(url,true, asw_error_counter);

        // else return false for no special action
        } else {
            a = false;
        }
        return a;
    }


//Opening URLs inside webview with request
    void aswm_view(String url, Boolean tab, int error_counter) {
        if(error_counter > 2){
            asw_error_counter = 0;
            aswm_exit();
        }else {
            if(tab){
                if(ASWP_TAB) {
                    CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();
                    intentBuilder.setToolbarColor(ContextCompat.getColor(this, R.color.colorPrimary));
                    intentBuilder.setSecondaryToolbarColor(ContextCompat.getColor(this, R.color.colorPrimaryDark));
                    intentBuilder.setStartAnimations(this, android.R.anim.slide_in_left, android.R.anim.slide_out_right);
                    intentBuilder.setExitAnimations(this, android.R.anim.slide_in_left, android.R.anim.slide_out_right);
                    CustomTabsIntent customTabsIntent = intentBuilder.build();
                    try {
                        customTabsIntent.launchUrl(MainActivity.this, Uri.parse(url));
                    } catch (ActivityNotFoundException e) {
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setData(Uri.parse(url));
                        startActivity(intent);
                    }
                }else{
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse(url));
                    startActivity(intent);
                }
            } else {
                if (url.contains("?")) { // check to see whether the url already has query parameters and handle appropriately.
                    url += "&";
                } else {
                    url += "?";
                }
                url += "rid=" + random_id();
                asw_view.loadUrl(url);
            }
        }
    }

如果我在“aswm_view(url,true, asw_error_counter);”中输入 False,则外部链接会在 webview 中打开,这是我不希望发生的情况。

提前致谢,如果需要,我可以分享更多代码。

编辑:我已根据请求添加了 aswm_view 方法

最佳答案

通过查看您的 aswm_view 方法,ASWP_TAB 变为 false 或找不到 MainActivity。

请在aswm_view方法中仔细检查两者。

关于java - 如何直接在应用内浏览器(Chrome 自定义选项卡)中打开外部链接,而不提示选择浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58048878/

相关文章:

Android WorkManager - 如何延迟任务?

android - 从数据库中获取值并将其显示在android中的TextView中

android - Android Studio仅在选择“生成APK”时签名APK,而在单击“运行”按钮时不签名

java - 如何在 fragment 中放置或替换列表

Java Check 工作簿包含特定电子表格或不使用 Apache POI

java - java中最终静态方法和变量如何存储以及在哪里存储?

java - 如何知道 .jar 文件中的哪些类被引用了?

java - Java 中 *= 运算符的运算符优先级是什么?

android - 双屏HDMI输出编程

android-studio - Android Studio 错误无法建立应用插件 com.android.application