Android webview flash playback (determine whether the flash plug-in is installed)

Source: Internet
Author: User

Recently, I helped a student develop a project and recorded some knowledge points intermittently. A webview exists in a page to play SWF. If the flash plug-in is not installed in the system, you must be prompted to install it in the market.

The following is a demo:

First, layout the file, which is simple:

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

Next, you must first check whether the Adobe Flash Player Plug-in has been installed in the system. The packagename of the plug-in is com. Adobe. flashplayer:

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;}

If it is not installed, you need to make a reminder. To provide a good experience, I load a simple HTML file through webview to remind you that the HTML file is placed under assets. 1:

There is a link in this HTML file. Clicking this link will jump to the market to download it. This involves calling Java local methods in Javascript. The following describes how to call it, in The onclick link above, it calls window. the gomarket () method of the android object. The window is opened in a normal browser. android is undefined, so we need to build such an object in Java. webview has

addJavascriptInterface(Object obj, String interfaceName);

Method. The first parameter is the object that we need to build and bind to Javascript. The second parameter is the name called in Javascript. Here is Android.

private void install() {mWebView.addJavascriptInterface(new AndroidBridge(), "android");mWebView.loadUrl("file:///android_asset/go_market.html");}

The constructed object is as follows:

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);}});}}

If JavaScript calls the gomarket () method, it enters a special thread dedicated to the browser. If the UI update is involved, it must be operated by handler. In this way, you will call the market software in your mobile phone after clicking the link. After installation, you can play the flash. There is an a.swf under assets, and 3:

String url="file:///android_asset/a.swf";mWebView.loadUrl(url);

Of course, you must set webview at the beginning:

WebSettings setting=mWebView.getSettings();setting.setPluginState(PluginState.ON);setting.setJavaScriptEnabled(true);

Hardware acceleration must be enabled for Android or later. In the manifest file, add the following attributes under the activity Tag:

android:hardwareAccelerated="true"

Package and download the demo.

Webview flash playback, which has many compatibility issues, is not recommended for me.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.