webView.getSettings().setJavaScriptEnabled(true);

webView.addJavascriptInterface(new JavascriptHandler, "testinterface");

 

class JavascriptHandler {

@JavascriptInterface

public void toToast(String text){

Toast.makeText(this, text, Toast.LENGTH_SHORT).show();

}

// @JavascriptInterface 無此註解的函式被呼叫也不會動作

public void toLog(String text){

Log.e(TAG,text);

}

}

 

在HTML端呼叫

javascript:window.testinterface.toToast("跳出訊息") 會動作

javascript:window.testinterface.toLog("顯示LOG") 不會動作,因為沒加註解

 

 

 

 

arrow
arrow
    全站熱搜

    小彬彬 發表在 痞客邦 留言(0) 人氣()