zoukankan      html  css  js  c++  java
  • 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播放还是参照网上的一个开源库实现的,这个在下一篇介绍


    博客地址:http://qiaoyihang.iteye.com/

  • 相关阅读:
    Spring 在xml配置里配置事务
    Spring事务的传播行为
    Spring 自动装配;方法注入
    Spring 依赖注入(一、注入方式)
    Spring事务
    C3P0使用详解
    php 解析json失败,解析为空,json在线解析器可以解析,但是json_decode()解析失败(原)
    Linux下的crontab定时执行任务命令详解
    crontab 常见 /dev/null 2>&1 详解
    实体字符转换,同样变量密码加盐MD5后生成的加密字符串不同解决办法 (原)
  • 原文地址:https://www.cnblogs.com/qiaoyihang/p/6166174.html
Copyright © 2011-2022 走看看