android播放swf文件

虽然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。

地址:https://pan.baidu.com/s/1eSCtDEa#list/path=%2F

转 : http://blog.csdn.net/yearafteryear/article/details/8959475#reply

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值