html載入swf,代碼類比點擊事件

來源:互聯網
上載者:User

標籤:android   style   class   code   java   http   

一、背景

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端添加安全路徑,到此為止已可實現想要的效果




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.