使用intent过滤器重定向Android webview

max*_*o87 6 android webview intentfilter

我有一个特定的网址,我希望从带有意图过滤器的网页视图重定向到我的应用中的特定活动.如何在Android上实现我自己的URI方案描述了如何为浏览器页面执行此操作,但是当通过webview访问该URL时,此相同的intent过滤器不起作用.还有什么需要添加到此intent过滤器以捕获这些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>`
Run Code Online (Sandbox Code Playgroud)

nur*_*eta 4

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

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

<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>
Run Code Online (Sandbox Code Playgroud)

当您单击 Web 视图中如下所示的链接时,您会期望打开 PretendChat 活动:“testing://chat”。为了实现这一点,您需要在您在 Web 视图上使用的 Web 视图客户端上添加以下代码。假设启动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);    


    }
}
Run Code Online (Sandbox Code Playgroud)