javascript - Web View 中的 Android Javascript 接口(interface)空对象

标签 javascript android webview android-4.4-kitkat

我正在尝试设置一个简单的 Android 应用程序,它显示一个 webview 并具有一些可通过 Javascript 接口(interface)使用的额外功能。

我正在运行 Kitkat(通过 Cyanogenmod)的 HTC One S 上进行测试。当我检查 webview 时,插入的对象 (Android) 是空的“对象 {}”。我无法通过加载的页面使用界面中的功能。

我构建了一个更复杂的 Activity ,但无法使界面正常工作,因此我创建了这个新的应用程序进行测试,其中仅包含 webview 指南中的代码。仍然没有将方法插入到暴露的对象中。

我的 Activity

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;


public class MyActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        WebView myWebView = (WebView) findViewById(R.id.webview);
        myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.loadUrl("http://47e148ba.ngrok.com");
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

网络应用接口(interface)

import android.content.Context;
import android.webkit.JavascriptInterface;
import android.widget.Toast;

/**
 * Created by graeme on 28/08/2014.
 */
public class WebAppInterface {
    Context mContext;

    /** Instantiate the interface and set the context */
    WebAppInterface(Context c) {
        mContext = c;
    }

    /** Show a toast from the web page */
    @JavascriptInterface
    public void showToast(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    }
}

布局

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />

构建.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 19
    buildToolsVersion "20.0.0"

    defaultConfig {
        applicationId "com.mydomain.jswebview"
        minSdkVersion 17
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

您可以看到我正在使用方法所需的装饰器,大多数问题似乎都将其作为解决方案。我正在 Android Studio 中开发,如果其中的任何预设可能会产生干扰?

编辑:

在 JS 文件中,或者如果我通过 chrome 检查 webview,如果我调用 Android 或 window.Android,或者通过控制台,我得到一个空对象返回“Object {}”。或者,如果我尝试使用上面接口(interface)中定义的方法,我将收到方法未定义错误。

最佳答案

我遇到了同样的问题。原来是Proguard问题。尝试将以下内容添加到您的 Proguard 规则中:

-keepclassmembers class ** {
    @android.webkit.JavascriptInterface <methods>;
}

关于javascript - Web View 中的 Android Javascript 接口(interface)空对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25548619/

相关文章:

javascript - 回调遇到困难

javascript - 保持 Logo 图像纵横比的灵活标题

android - 如何配对和连接耳机

java - 如何获取当前时间附近的时间 [Android Studio]

按下按钮或按下事件后,Flutter 授予摄像头和麦克风权限

android - WebView 和本地主机

javascript - 如何通过react在express上向mailchimp发送post请求

Android 数据绑定(bind)构建错误 : [data binding plugin]: failed to setup data binding

android - Android 中的 Webview 建立内存

Javascript:为什么无法在回调函数中获取变量?