Android使用WebView播放flash的方法和JavaScript调用本地方法
生活随笔
收集整理的這篇文章主要介紹了
Android使用WebView播放flash的方法和JavaScript调用本地方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <WebView android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_centerInParent="true" android:scrollbars="none"/> </RelativeLayout>2.先檢測系統中是否已經安裝了 adobe flash player 插件,插件的 packageName 是 com.adobe.flashplayer
private boolean check() {PackageManager pm = getPackageManager(); List<PackageInfo> infoList = pm.getInstalledPackages(PackageManager.GET_SERVICES); for (PackageInfo info : infoList) {if ("com.adobe.flashplayer".equals(info.packageName)) {return true; }}return false; }3.如果未安裝,則要做出提醒,通過WebView加載html文件來提醒,html文件放在assets下
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="Generator" content="EditPlus?"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> <title>Document</title> </head> <body> <h3>沒有安裝 adobe flash player!</h3> <p> <a href="http://as.sogou.com/detail?pid=34&cid=52&docid=8229834026150749159&e=1969" οnclick="window.android.goMarket()">市場安裝</a> </p> <p> <a href="http://116.62.162.56/pjriver/flashplayer/Flashbofangqi.apk" οnclick="window.android.goMarket()">在線安裝</a> </p> </body> </html>4.html文件中有一個鏈接,點擊這個鏈接則會跳轉到市場中去下載,涉及到在Javascript中調用Java本地方法,下面如何調用,看上面鏈接的onclick中,它調用window.android對象的goMarket()方法,普通瀏覽器打開這個頁面的,window.android是未定義的,則我們就需要在Java中構建這么一個對象,WebView有一個方法,第一個參數就是我們需要構建的綁定到javascript的那個對象,第二個參數就是在javascript中調用的名字,這兒就是android。
@SuppressLint("JavascriptInterface") private void install() {mWebView.addJavascriptInterface(new AndroidBridge(), "android"); mWebView.loadUrl("file:///android_asset/go_market.html"); }5.構建的對象
private class AndroidBridge {public void goMarket() {handler.post(new Runnable() {public void run() {Intent installIntent = new Intent("android.intent.action.VIEW"); installIntent.setData(Uri.parse("market://details?id=com.adobe.flashplayer")); startActivity(installIntent); }}); } }6.javascript如果調用goMarket()方法,進入瀏覽器的特殊線程,如果涉及的UI更新,則要通過handler來操作。安裝之后就能播放flash
String url="http://flash.tool.hexun.com/flash_ma/20140901-Futures/qh_data.html?c=HCMI&m=HEXUN&e=hfi"; mWebView.loadUrl(url); WebSettings setting=mWebView.getSettings(); setting.setPluginState(PluginState.ON); setting.setJavaScriptEnabled(true);7.android4.0以上還得開啟硬件加速,在manifest文件中,這個activity標簽下加上如下屬性
android:hardwareAccelerated="true" Demo下載地址:https://download.csdn.net/download/qq_39735504/10297328總結
以上是生活随笔為你收集整理的Android使用WebView播放flash的方法和JavaScript调用本地方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html设置三号字体是多少px,CSS
- 下一篇: DT时代,企业要有“经营安全、安全经营”