最近帮一个同学做一个项目,断断续续的一些知识点记录一下。一个页面中有一个WebView,用来播放swf,如果系统中未安装flash插件,必须提示用户到market中安装。
下面做一个demo,效果图如下:
首先布局文件,很简单:
- <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>
- private boolean check() {
- 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;
- }
- <html>
- <head></head>
- <body>
- <h3>Not install adobe flash player!</h3>
- <p>
- <a href="#" onclick="window.android.goMarket()">go market to install.</a>
- </p>
- </body>
- </html>
- addJavascriptInterface(Object obj, String interfaceName);
- private void install() {
- mWebView.addJavascriptInterface(new AndroidBridge(), "android");
- mWebView.loadUrl("file:///android_asset/go_market.html");
- }
- private class AndroidBridge {
- public void goMarket() {
- handler.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);
- }
- });
- }
- }
- String url="file:///android_asset/a.swf";
- mWebView.loadUrl(url);
- WebSettings setting=mWebView.getSettings();
- setting.setPluginState(PluginState.ON);
- setting.setJavaScriptEnabled(true);
- android:hardwareAccelerated="true"
WebView播放flash,兼容问题还不少,我不太推荐使用。