展会信息港展会大全

android webview and javascript交互
来源:互联网   发布日期:2015-10-02 21:35:32   浏览:1384次  

导读:方法一:*javascript in html:<SCRIPT LANGUAGE="JavaScript">function checkOrientation(){愀氀攀爀琀(wi...

方法一:

*javascript in html:

<SCRIPTLANGUAGE="JavaScript">

function checkOrientation(){

alert(window.orientation);

};

window.addEventListener('onorientationchange' in window ? 'orientationchange' : 'resize', checkOrientation, false);

function print() {

window.location="print://print";

};

function setting() {

window.location="setting://setting";

};

</SCRIPT>

*java code:

public boolean shouldOverrideUrlLoading(WebView view, String url) {

AppUtils.LogD(url);

if (url.startsWith("mailto:")) {

MailTo mt = MailTo.parse(url);

Intent i = AppUtils.newEmailIntent(activity, mt.getTo(),

mt.getSubject(), mt.getBody(), mt.getCc());

activity.startActivity(i);

view.reload();

return true;

} else if (url.startsWith("print://print")) {

// show toast message for print button

Toast.makeText(activity, R.string.print_button_alert,

Toast.LENGTH_LONG).show();

return true;

} else if (url.startsWith("setting://setting")) {

// show setting dialog

((DetailViewControlerImpl) activity).openSettingsPage();

return true;

} else {

return false;

}

}

方法二:

javascript in html(mypage):

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<HTML>

<HEAD>

<TITLE>Actuate Viewer</TITLE>

<script type="text/javascript">

function change() {

document.getElementById("changePhoto").src = "http://up.2cto.com/2012/0519/20120519094025145.png";

}

</script>

</HEAD>

<BODY>

<a onclick="window.mypage.clickOnAndroid()"> <img id="changePhoto"

src="http://up.2cto.com/2012/0519/20120519094025304.png" /><br> Click me! </a>

</BODY>

</HTML>

java code:

mWebView.addJavascriptInterface(new MyJavaScriptInterface(), "mypage");

final class MyJavaScriptInterface {

public MyJavaScriptInterface() {

}

public void clickOnAndroid() {

mHandler.post(new Runnable() {

@Override

public void run() {

mWebView.loadUrl("javascript:change()");

}

});

}

}

摘自 fhy_2008的专栏

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港