一、背景
1、最近在做一个android端播放flash的应用,android设备没有屏幕,显示需通过HDMI连接。需要用到javascript与java之间的交互,android端接收flash传过来的fscommand
2、希望通过WebView加载html实现
二、遇到问题
1、通过html加载之后的动画,无论按哪个按键,swf文件都接收不到按键消息,既不能实现它本身的一些操作性功能
2、传过来的命令接收不到(有时间再分析这个,已经解决)
三、尝试方法
1、发现如果直接通过电脑端使用浏览器打开该html,直接敲键盘也是没有效果的,可鼠标点击一下,按键却有效果了
2、没有收到命令,通过添加flash安全沙箱路径即可,当然,你得安装flash player
3、通过猜测觉得应该是WebView开始的时候没有获取到焦点,所以按键事件一直没有捕捉
四、成功解决
1、html加载main.swf
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd ">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="JavaScript" type="text/JavaScript">
var str1 = "hello";
var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
function myFlash_DoFSCommand(command, args) {
var myFlashObj = InternetExplorer ? myFlash : document.myFlash;
if(command == "back"){
//alert(command);
doFromCommand(command);
}else{
alert(command);
}
}
if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 &&
navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
document.write('<SCRIPT LANGUAGE=VBScript\> \n');
document.write('on error resume next \n');
document.write('Sub myFlash_FSCommand(ByVal command, ByVal args)\n');
document.write(' call myFlash_DoFSCommand(command, args)\n');
document.write('end sub\n');
document.write('</SCRIPT\> \n');
}
function doFromCommand(command){
window.myjs2.runJs2Activity(command);//调用android的函数
}
</script>
</head>
<body>
<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=11,0,0,0"
WIDTH="100%" HEIGHT="100%" id="myFlash">
<PARAM NAME=movie VALUE="main.swf">
<PARAM NAME=quality VALUE=high>
<PARAM NAME=bgcolor VALUE=#CCCCCC>
<param name="allowScriptAccess" value="always" />
<param name="allowNetworking" value="all">
<param name="allowFullScreen" value="true">
<EMBED src="main.swf" quality=high bgcolor=#CCCCCC WIDTH="100%" HEIGHT="100%" NAME="myFlash" swLiveConnect="true" allowScriptAccess="always" allownetworking="all" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED>
</OBJECT>
<div id="show" style="color:#000000;font-size:30px;margin-left:50px" ></div>
</body>
</html>
其中:
WIDTH="100%" HEIGHT="100%"
这个是根据设备撑满屏幕,针对在不同分辨率不同屏幕大小的设备来说,该属性是必不可少的
2、模拟点击事件,可在html加载成功之后加入下面代码
mWebView.dispatchTouchEvent(MotionEvent.obtain(
SystemClock.uptimeMillis(), SystemClock.uptimeMillis(),
MotionEvent.ACTION_DOWN, mWebView.getLeft() + 5,
mWebView.getTop() + 5, 0));
mWebView.dispatchTouchEvent(MotionEvent.obtain(
SystemClock.uptimeMillis(), SystemClock.uptimeMillis(),
MotionEvent.ACTION_UP, mWebView.getLeft() + 5,
mWebView.getTop() + 5, 0));
3、android端添加安全路径,到此为止已可实现想要的效果