Android SDK中的WebView呼叫活动 [英] Android SDK WebView call Activity

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

问题描述

我想点击一个的WebView组件中的链接时,启动一个活动。

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

我的web视图里面 Main.java 加载,我想推出 SubActivity.java 点击一个链接时,这是在 Main.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?

示例检查://项目/ 1

检查是我的应用程序的名称,检查的活动,我想发动和 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的<一个href="http://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface%28java.lang.Object,java.lang.String%29">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");

然后在JavaScript从web视图你只需要使用新的Android的对象的launchIntent的方法。

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

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

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