Android SDK WebView 调用Activity [英] Android SDK WebView call Activity

查看:28
本文介绍了Android SDK WebView 调用Activity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在单击 WebView 组件内的链接时启动 Activity.

I'm trying to launch an Activity when clicking a link inside a WebView component.

我的 Webview 加载到 Main.java 中,我想在点击位于 Main.java 中的网站内的链接时启动 SubActivity.java?

My Webview is loaded inside Main.java and I would like to launch SubActivity.java when clicking a link inside the Website which is in Main.java?

另外,我如何将参数传递给这个活动?

Also, how can I pass parameters to this activity?

示例:inspection://Project/1

Inspection"是我的应用程序的名称,inspection 是我想要启动的活动,1 是我想要的 ID.

"Inspection" is the name of my application, inspection is the Activity I would like to launch and 1 is the ID I would like to have.

推荐答案

你可以使用 WebView 的 addJavaScriptInterface 以允许 JavaScript 控制您的应用程序(在这种情况下,允许 JavaScript 在单击链接时触发 Intent).

You could use WebView's addJavaScriptInterface to allow JavaScript to control your application (in this case, to allow JavaScript to fire an Intent when a link is clicked).

为此,您需要传递一个类实例以绑定到 JavaScript,这可能类似于以下内容:

To do this you need to pass a class instance to bind to JavaScript, this could be something like the following:

private final class JsInterface {
      public void launchIntent(final String payload) {
         Activity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
               // use the payload if you want, attach as an extra, switch destination, etc.
               Activity.this.startActivity(new Intent(Activity.this, SomeOtherActivity.class));
            }
         });
      }
   }

然后您将其添加到 WebView 中,内容如下:

Then you add that to the WebView with something along these lines:

webView.addJavascriptInterface(js, "Android");

然后在来自 WebView 的 JavaScript 中,您只需使用新的Android"对象的launchIntent"方法.

Then in JavaScript from the WebView you just use your new "Android" object's "launchIntent" method.

这篇关于Android SDK WebView 调用Activity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆