虽然Adobe已经宣称不再支持android下的flash播放了,但是在HTML5没有热火起来之前还是有许多地方需要用到flash的,比如希望在android机子上使用以前flash制作的一些游戏或者教学动画。这里写了个demo使用android的WebView控件和Adobe的flash插件来播放本地的卡丁车的swf文件,在android机子上插上USB键盘就可以在android平台机子上玩了。
首先上个播放的效果图
代码架构
布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <WebView android:id="@+id/flashwebview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
package com.example.flashplayer; import java.util.List; import android.net.Uri; import android.os.Bundle; import android.os.Handler; 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.view.Menu; import android.webkit.WebChromeClient; import android.webkit.WebSettings.PluginState; import android.webkit.WebView; import android.widget.ProgressBar; public class MainActivity extends Activity { private WebView mWebView; private Handler mHandler=new Handler(); private String mFlashFilename; private ProgressDialog mProgressDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mWebView=(WebView)findViewById(R.id.flashwebview); setTitle("flash播放器"); setTitleColor(Color.RED); mWebView.getSettings().setPluginsEnabled(true); mWebView.getSettings().setPluginState(PluginState.ON); Intent intent = getIntent(); String str = intent.getStringExtra("flashName"); if(str==null) mFlashFilename=new String("file:///android_asset/kaka.swf"); else mFlashFilename=str; 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()){ mWebView.loadUrl(mFlashFilename); }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.activity_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 private void installadobeapk() { mWebView.addJavascriptInterface(new AndroidBridge(), "android"); mWebView.loadUrl("file:///android_asset/go_market.html"); } 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); } }); } } }
PS:需要在android机子上安装Adobe_Flash_Player_11.1.112.61.apk,android系统版本是4.0.3。
转 : http://blog.csdn.net/yearafteryear/article/details/8959475#reply