android播放swf檔案

weixin_34234721發表於2014-01-16

雖然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

轉載於:https://www.cnblogs.com/622698abc/p/3523225.html

相關文章