【问题标题】:Redirect Android webview with intent filter使用意图过滤器重定向 Android webview
【发布时间】:2013-07-28 06:12:17
【问题描述】:

我有一个特定的 URL,我想将它从带有意图过滤器的 web 视图重定向到我的应用程序中的特定活动。 How to implement my very own URI scheme on Android 描述了如何为浏览器页面执行此操作,但是当通过 web 视图访问该 URL 时,相同的意图过滤器不起作用。是否需要将其他任何内容添加到此意图过滤器以捕获这些 webview 链接?

<intent-filter>
    <action android:name="android.intent.action.VIEW"></action>
    <category android:name="android.intent.category.DEFAULT"></category>
    <category android:name="android.intent.category.BROWSABLE"></category>
    <data android:host="myurl.com/stuff" android:scheme="http"></data>
  </intent-filter>`

【问题讨论】:

    标签: android webview intentfilter


    【解决方案1】:

    我没有意图过滤器和 webview 一起工作,只是在清单上声明意图,我认为他们不应该这样做。 (我想知道为什么......)我认为这样做的方法是在您尝试在 webview 中打开它们并创建意图时捕获 url。

    然后,对于清单中的活动注册如下:

    <activity android:name=".PretendChat">
            <intent-filter>
                <action android:name="android.intent.action.VIEW"></action>
                <category android:name="android.intent.category.DEFAULT"></category>
                <category android:name="android.intent.category.BROWSABLE"></category>
                <data android:host="chat" ></data>
                <data android:scheme="testing"></data>
                <data android:pathPattern=".*"></data>
            </intent-filter>
        </activity>
    

    当您单击如下链接时,您会期望 PretendChat 活动会打开:webview 中的“testing://chat”。为了让那发生你 您在 webview 上使用的 webview 客户端上需要以下代码。假设启动 webview 的 Activity 称为 WebviewActivity。

    private  class TestWebViewClient extends WebViewClient       {
    
    
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
    
            try {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                WebviewActivity.this.startActivity(intent);
    
    
            }   catch(ActivityNotFoundException e) {
    
                Log.e(LOGTAG,"Could not load url"+url);
            }
    
            return super.shouldOverrideUrlLoading(view, url);    
    
    
        }
    }
    

    【讨论】:

    • 此解决方案仅在您可以控制 webview 的情况下有效,即您正在覆盖 webView 对象实现源代码的位置。当这个 webview 是 compile/jar/libarary 形式并且你没有源代码来修改它时怎么办。那么我们将如何根据特定的 URL 重定向到一些活动,这就是问题
    【解决方案2】:

    我让它工作的唯一方法是使用虚拟文件链接并使用 loadDataWithBaseURL 加载 URL 并使用数据中的相对链接。最新的 WebView 似乎只接受有效链接,我无法让它与活动的自定义意图一起使用。

    如果我尝试不同的方案,例如“my-app”而不是“file”,则 shouldOverrideUrlLoading 中的 url 会显示为“about:blank”。

    我还想将参数传递给要在 Bundle 中打开的活动。

                webView.loadDataWithBaseURL("file:///android_asset", w.Definition, "text/html", "utf-8", null);
                webView.setWebViewClient(new WebViewClient() {
                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view, String url) {
                        if (url.startsWith("file")) {
                            Intent intent = new Intent(DictWordActivity.this, DictWordActivity.class);
                            intent.putExtra("word", Uri.parse(url).getLastPathSegment());
                            startActivity(intent);
                            return true;
                        } else
                            return false;
                    }
                });
    

    我创建了这样的相对链接:

    "<a href=\"" + word + "\">" + word + "</a>");
    

    如果有人有更好的解决方案,我想知道。

    【讨论】:

      猜你喜欢
      • 2016-07-09
      • 2015-05-14
      • 1970-01-01
      • 2012-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-28
      • 1970-01-01
      相关资源
      最近更新 更多