加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

打开本地flash webview open swf

发布时间:2020-12-15 07:04:35 所属栏目:百科 来源:网络整理
导读:想打开flash文件,必须装有flash插件。android手机上装Adobe Flash Player .APK才能打开显示flash。 可以单独成为android工程,也可以打包成jar,放到unity调用。 单独android工程需把swf文件放到assets文件夹里 MainActivity.class package com.example.ope

想打开flash文件,必须装有flash插件。android手机上装Adobe Flash Player .APK才能打开显示flash。

可以单独成为android工程,也可以打包成jar,放到unity调用。


单独android工程需把swf文件放到assets文件夹里

MainActivity.class

package com.example.openswf_jar;

import java.util.List;

import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.util.Log;
import android.view.Menu;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;

public class MainActivity extends Activity {

	private WebView mWebView;
	private Handler mHandler=new Handler();
	private String mFlashFilePath;
	private ProgressDialog mProgressDialog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mWebView=(WebView)findViewById(R.id.webview);
		setTitle("flash播放器");
		setTitleColor(Color.RED);
		
		WebSettings setting=mWebView.getSettings();
		setting.setPluginState(PluginState.ON);
		setting.setJavaScriptEnabled(true);
		
		Intent intent = getIntent();
		mFlashFilePath = intent.getStringExtra("url");
		if(mFlashFilePath == null)
			mFlashFilePath = "file:///android_asset/flash.swf";

		try {
			Thread.sleep(500);// 主线程暂停下,否则容易白屏,原因未知
		} catch (InterruptedException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		
		mProgressDialog=ProgressDialog.show(this,"请稍等...","加载flash中...",true);
		mWebView.setWebChromeClient(new WebChromeClient(){
			@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);
				}
			}
		});
		if(checkinstallornotadobeflashapk()){
			Log.i("mainactivity","OPENSWF"+mFlashFilePath);
			mWebView.loadUrl(mFlashFilePath);
		}else{
			installadobeapk();
		}
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main,menu);
		return true;
	}

	//退出时关闭flash播放
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mWebView.destroy();
		this.finish();
		System.gc();
	}
	//按下Back按键时关闭flash播放
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		mWebView.destroy();
		this.finish();
		System.gc();
		super.onBackPressed();
	}
	//后台运行
	@Override
	protected void onUserLeaveHint() {
		// TODO Auto-generated method stub
		mWebView.destroy();
		this.finish();
		System.gc();
		super.onUserLeaveHint();
	}
	//检查机子是否安装的有Adobe Flash相关APK 
	private boolean checkinstallornotadobeflashapk() {
		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;
	}
	
	//安装Adobe Flash APK
	@SuppressLint("JavascriptInterface") 
	private void installadobeapk() {
		mWebView.addJavascriptInterface(new AndroidBridge(),"android");
		mWebView.loadUrl("http://app.mi.com/detail/188");
	}
	private class AndroidBridge {
		public void goMarket() {
			mHandler.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);
				}
			});
		}
	}
}

想加入到unity中,就要写以下内容。把swf文件放到StreamingAssets文件夹里。

UnityCall.class

package com.example.openswf_jar;

import android.content.Context;
import android.content.Intent;

public class UnityCall {

	public void U3DCallOpenSWF(Context unityActivity,String url)
	{
		Intent intent = new Intent(unityActivity,MainActivity.class);
		intent.putExtra("url",url);
		unityActivity.startActivity(intent);
	}
}


在unity中写代码

<span style="white-space:pre">	</span>private AndroidJavaObject javaObj = null;
	
	private AndroidJavaObject GetJavaObject()
	{
		if (javaObj == null)
		{
			javaObj = new AndroidJavaObject("com.example.openswf_jar.UnityCall");
		}
		
		return javaObj;
	}

	void OpenSWF()
	{
		AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
		AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
		GetJavaObject().Call("U3DCallOpenSWF",jo,"file:///android_asset/flash.swf");
	}

activity_main.xml

<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" />
</RelativeLayout>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读