如何通过webview调用javascript函数?

时间:2013-08-26 09:08:07

标签: android

我在调用invokeScriptMethod(" showPoll(" + Device.PollID +");")时有两条消息; 并且不起作用!

logcat中的

消息 1 - 防火墙不为空 2-euler:isUrlBlocked = false

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_poll);     
    Device.webViewShowPoll = (WebView) findViewById(R.id.webViewShowPoll);      
    Device.webViewShowPoll.getSettings().setJavaScriptEnabled(true);        
    Device.webViewShowPoll.getSettings().setPluginsEnabled(true);           
    Device.webViewShowPoll.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    Device.webViewShowPoll.getSettings().setAllowFileAccess(true);
    Device.webViewShowPoll.addJavascriptInterface(appconnector, "appConnector");

    try
    {
        Device.webViewShowPoll.loadDataWithBaseURL("file:///android_asset/",
                Utility.convertStreamToString(getAssets().open("application.html",
                        AssetManager.ACCESS_BUFFER)), "text/html", "utf-8","");         
    } 
    catch (IOException e)
    {
        Log.e(getString(R.string.app_name), e.getMessage());
    }
}   

public static void invokeScriptMethod(String method) {

    Device.webViewShowPoll.loadUrl("javascript:" + method);
}

public static void invokeScript(String script) {
    Device.webViewShowPoll.loadUrl(script);
}   

public void bt_click(View v)
{
    invokeScriptMethod("showPoll(" + Device.PollID + ");");
}

和AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.omid.epoll.mobile"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8"
android:targetSdkVersion="16" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SET_DEBUG_APP"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />    
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />    

1 个答案:

答案 0 :(得分:0)

可能这行代码有助于:webView.getSettings().setPluginState(PluginState.ON);