是否可以在 WebView android 应用程序的浏览器中仅打开一个特定的 link?

Is it possible to open only one specific link within a browser within a WebView android app?

我最近一直在尝试涉足 Android 应用程序,并且我创建了一个简单的 Web 视图应用程序来打开我的网站。一切都按预期工作,我能够通过下面的代码在应用程序本身中打开 links。

    public boolean shouldOverrideUrlLoading(WebView webView, String url) {
        return false;
    }

我很好奇我是否只能在浏览器中打开特定的 link,但是?

我尝试使用

if (url.equals("my url here")) {
          //
}

在我上面提供的方法中,但我不知道在我的 if 语句中调用什么。我试过上网查看一下,但我发现的大多数方法似乎都已被弃用。

我希望我定义的 URL 在默认浏览器中打开,但其他所有内容都在应用程序中打开。

感谢您的帮助!

编辑:WebViewClientImpl class

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class WebViewClientImpl extends WebViewClient {

    private Activity activity = null;
    private android.content.Context Context;

    public WebViewClientImpl(Activity activity) {
        this.activity = activity;
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView webView, String url) {
        if (url.equals("my url here")) {
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            startActivity(i);

            return true;
        }
        return false;
    }
}

是的,这很有可能。应该这样做:

public boolean shouldOverrideUrlLoading(WebView webView, String url) {
    if (url.equals("my url here")) {
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(url));
        startActivity(i);

        return true;
    }

    return false;
}

请注意,在通过浏览器加载 URL 的情况下,您将 return true 取消在 WebView 中加载它。

试试这个:

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class WebViewClientImpl extends WebViewClient {

   Context context;

    public WebViewClientImpl(Context context1) { //pass activity here 
        this.context= context1;
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView webView, String url) {
        if (url.equals("my url here")) {
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            context.startActivity(i);

            return true;
        }
        return false;
    }
}

如果您仍然感到困惑,请将 class 放入 activity。