android中基于HTML模板的方式嵌入SWF

继上一篇

利用webview实现在andorid中嵌入swf

这篇继续说说通过html模板的方式来嵌入SWF,这样做的好处最直观的就是可以把html,swf和android代码串起来,交互操作很方便(虽然这样最后没实现我最终的需求)

代码比较简单,上代码自己看吧

MainActivity

package com.example.flashdemo;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;

public class MainActivity extends Activity {

	private FrameLayout mFullscreenContainer;
	private FrameLayout mContentView;
	private View mCustomView = null;
	private WebView mWebView;
	private Handler mHandler = new Handler();
	private ProgressDialog mProgressDialog;  

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initViews();
		initWebView();

		if (getPhoneAndroidSDK() >= 14) {// 4.0需打开硬件加速
			getWindow().setFlags(0x1000000, 0x1000000);
		}

		mWebView.addJavascriptInterface(new DemoJavaScriptInterface(), "demo");
		mWebView.loadUrl("file:///android_asset/videoTem.html");
//		mWebView.loadUrl("file:///android_asset/NewSampleClient.html");
	}

	private void initViews() {
		mFullscreenContainer = (FrameLayout) findViewById(R.id.fullscreen_custom_content);
		mContentView = (FrameLayout) findViewById(R.id.main_content);
		mWebView = (WebView) findViewById(R.id.webview_player);
		//如果webView中需要用户手动输入用户名、密码或其他,则webview必须设置支持获取手势焦点。
		mWebView.requestFocusFromTouch();
	}

	private void initWebView() {
		WebSettings settings = mWebView.getSettings();
		//设置此属性,可任意比例缩放
		settings.setUseWideViewPort(true);//将图片调整到适合webview的大小 
		settings.setLoadWithOverviewMode(true);
		
		//打开页面时, 自适应屏幕:
		settings.setBuiltInZoomControls(true);
		settings.setSupportZoom(true);//支持缩放 
		
		
		settings.setJavaScriptEnabled(true);//支持js
		settings.setJavaScriptCanOpenWindowsAutomatically(true);
		settings.setPluginState(PluginState.ON);
		settings.setPluginsEnabled(true);//支持插件 
		settings.setAllowFileAccess(true);

		mProgressDialog=ProgressDialog.show(this, "请稍等...", "加载flash中...", true);  
		mWebView.setWebChromeClient(new MyWebChromeClient());
		mWebView.setWebViewClient(new MyWebViewClient());
	}
	
	class MyWebChromeClient extends WebChromeClient {
		
		private CustomViewCallback mCustomViewCallback;
		private int mOriginalOrientation = 1;
		@Override
		public void onShowCustomView(View view, CustomViewCallback callback) {
			// TODO Auto-generated method stub
			onShowCustomView(view, mOriginalOrientation, callback);
			super.onShowCustomView(view, callback);
			
		}
		
        public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());

            builder.setTitle("OA提示信息")
            .setMessage(message)
            .setPositiveButton("确定", null)
            .setCancelable(false)
            .create()
            .show();
            result.confirm();
            return true;
        }
        
        @Override  
        public void onProgressChanged(WebView view, int newProgress) {  
            // TODO Auto-generated method stub  
            super.onProgressChanged(view, newProgress);  
            System.out.println("newProgress:"+String.valueOf(newProgress));  
            if(newProgress==100){  
                new Handler().postDelayed(new Runnable() {  
                      
                    @Override  
                    public void run() {  
                        // TODO Auto-generated method stub  
                        mProgressDialog.dismiss();  
                    }  
                }, 500);  
            }  
        }  

		@SuppressLint("Override")
		public void onShowCustomView(View view, int requestedOrientation,
				WebChromeClient.CustomViewCallback callback) {
			if (mCustomView != null) {
				callback.onCustomViewHidden();
				return;
			}
			if (getPhoneAndroidSDK() >= 14) {
				mFullscreenContainer.addView(view);
				mCustomView = view;
				mCustomViewCallback = callback;
				mOriginalOrientation = getRequestedOrientation();
				mContentView.setVisibility(View.INVISIBLE);
				mFullscreenContainer.setVisibility(View.VISIBLE);
				mFullscreenContainer.bringToFront();

				setRequestedOrientation(mOriginalOrientation);
			}

		}

		public void onHideCustomView() {
			mContentView.setVisibility(View.VISIBLE);
			if (mCustomView == null) {
				return;
			}
			mCustomView.setVisibility(View.GONE);
			mFullscreenContainer.removeView(mCustomView);
			mCustomView = null;
			mFullscreenContainer.setVisibility(View.GONE);
			try {
				mCustomViewCallback.onCustomViewHidden();
			} catch (Exception e) {
			}
			// Show the content view.

			setRequestedOrientation(mOriginalOrientation);
		}

	}

	class MyWebViewClient extends WebViewClient {

		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);
			return super.shouldOverrideUrlLoading(view, url);
		}

	}

	public static int getPhoneAndroidSDK() {
		int version = 0;
		try {
			version = Integer.valueOf(android.os.Build.VERSION.SDK);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return version;

	}
	
	final class DemoJavaScriptInterface {

        DemoJavaScriptInterface() {
        }

        /**
         * This is not called on the UI thread. Post a runnable to invoke
         * loadUrl on the UI thread.
         */
        public void clickOnAndroid() {
        	mHandler.post(new Runnable() {
                public void run() {
                    mWebView.loadUrl("javascript:wave('111','192.168.0.28')");
                }
            });

        }
    }
	
	

}

模板也提供一下

<script>
function getPolicyNo()
{
	alert(111);
	//var policyNo = policyNodocument.getElementById("policyNo").value;
	//return policyNo;
}
function getPrimaryServerIp()
{
	//var primaryServerIp = document.getElementById("primaryServerIp").value
	//alert(primaryServerIp);
	//return primaryServerIp;
}
function wave(policyNo,primaryServerIp) {
	document.getElementById("policyNo").value = policyNo;
	document.getElementById("primaryServerIp").value = primaryServerIp;

}

</script>
<html>

<head>

    <meta charset="utf-8" />

    <title>swf</title>

</head>

<body onload="window.demo.clickOnAndroid()">

     <embed src="NewSampleClient.swf"  
      bgcolor="#000000"  width="80%" height="80%" align="middle" 
      allowScriptAccess="always" allowFullScreen="true" wmode="transparent" 
      type="application/x-shockwave-flash"> </embed>
      
      <input type="hidden" id="policyNo"  value="" />
      <input type="hidden" id="primaryServerIp"  value="" />

</body>

</html>

布局文件也弄一下

<FrameLayout  xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"  
       android:layout_height="match_parent">
  
   <FrameLayout  
       android:id="@+id/fullscreen_custom_content"  
       android:layout_width="match_parent"  
       android:layout_height="match_parent"  
       android:visibility="gone" />  
  
   <FrameLayout  
       android:id="@+id/main_content"  
       android:layout_width="match_parent"  
       android:layout_height="match_parent">  
  
       <WebView  
           android:id="@+id/webview_player"  
           android:layout_width="match_parent"  
           android:layout_height="match_parent"  
           android:scrollbars="horizontal" />  
    </FrameLayout > 
</FrameLayout>

最新实现实时RTMP播放还是参照网上的一个开源库实现的,这个在下一篇介绍

相关推荐