How Android uses WebView to play Flash _android

Source: Internet
Author: User

This example describes how Android uses WebView to play flash and to determine whether to install Flash plug-ins. Share to everyone for your reference. The implementation methods are as follows:

First, the question:

Recently helped a classmate to do a project, intermittent some knowledge points record. There is a webview on a page to play SWF, and if the Flash plugin is not installed in the system, the user must be prompted to install in market.

Second, the solution:

Here is a demo, the effect is as follows:
Figure 1:

Figure 2:

Figure 3:

First, the layout file is simple:

Copy Code code as follows:
<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>

Then the activity, must first detect whether the system has installed Adobe Flash Player plug-in, plug-in PackageName is Com.adobe.flashplayer:
Copy code code as follows:
         Packagemanager PM = Getpackagemanager (); 
        list< packageinfo> infolist = pm 
                 getinstalledpackages (packagemanager.get_services); 
         for (PackageInfo info:infolist) { 
         & nbsp;  if ("Com.adobe.flashplayer". Equals (Info.packagename)) { 
                 return true; 
            } 
       } 
 & nbsp;      return false; 
}

If it is not installed, then to make a reminder, in order to provide a good experience, I passed WebView load a simple HTML file to remind, HTML file placed under assets, as shown in figure
Copy Code code as follows:
<body>
<p>
<a href= "#" onclick= "Window.android.goMarket ()" >go Market to Install.</a>
</p>
</body>

This HTML file has a link, click on the link will jump to the market to download, which involves invoking the Java local method in JavaScript, the following say how to call, look at the link above the onclick, It calls the Window.android object's Gomarket () method, the normal browser opens the page, window.android is undefined, then we need to build such an object in Java, WebView has a
Copy Code code as follows:
Addjavascriptinterface (Object obj, String InterfaceName);

method, the first argument is the object that we need to build to bind to JavaScript, and the second is the name that is invoked in JavaScript, and this is Android.
Copy Code code as follows:
private void Install () {
Mwebview.addjavascriptinterface (New Androidbridge (), "Android");
Mwebview.loadurl ("file:///android_asset/go_market.html");
}

The objects we build are as follows:
Copy Code code 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 browser-specific thread and, if the UI update is involved, operates through handler. Clicking on the link will then call the market software on your phone, as shown in Figure Ii. Flash can be played after installation. There is a a.swf under assets, as shown in Figure three.
Copy Code code as follows:
String url= "file:///android_asset/a.swf";
Mwebview.loadurl (URL);
Of course, in the beginning, you have to make some settings for WebView:

WebSettings setting=mwebview.getsettings ();
Setting.setpluginstate (Pluginstate.on);
Setting.setjavascriptenabled (TRUE);


Android4.0 above also has to turn on hardware acceleration, in the manifest file, the activity tag, add the following attributes:
Copy Code code as follows:
Android:hardwareaccelerated= "true"

Demo full instance code click here to download this site.

WebView play Flash, there are many compatibility issues, I do not recommend the use.

I hope this article will help you with your Android program.

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.