As we already know by now the HTC Hero supports Flash in the browser, and by double tapping on Flash content it will be played in full screen mode. In fact I believe that’s also the reason why we cannot use the Flash Player directly with Intents.
Those of you familiar with Nokia’s WebRuntime or the iPhone UIWebView will recognize WebView, because it’s the same thing. Really it’s an implementation of the browser framework made available as a UI component. Some of these implementations come with hooks into the platform code through JScript, and enable the use of device APIs like Location.
So what can we do with the Android browser? Let’s look at the pieces provided by Android:
- WebView – Used to load and display web pages using the built-in device browser and chrome, embedded into your application.
- WebViewClient – Enables the handling of various browser actions like page loading and error handling. Overrides the Activity in the built in browser.
- WebChromeClient – Enables the replacement of the browser chrome for events like progress, alerts and for window controls. Can override the default Chrome.
Step 1: Launch the built-in browser using WebView
The code to do this couldn’t be simpler, but there are a few extra changes to make with the layout declarative XML and with the AndroidManifest.xml. Android supports the ability to lay out the application using standard components, much like Flex.
To add a WebView to our layout we must add the WebView node to the main.xml (in project/assets/layout). You can see the id property of the node looking rather special, and that’s because it’s indicating a binding.. again, just like Flex. We can use this resource, and any others later in our code using the builtin R class.
Now we’ve done that we have to add a new permission to the Android Manifest.xml. You’ll notice that the XML includes a number of nodes pertaining to my application, most of it is easy to understand. Note also that my application has it’s own Intent and an Activity called StubApp.
With that we only need to add a tiny piece of code to use the webview component, check it out.
As before we overload the onCreate method, call the parent for good measure and then get to business. We call setContentView to build the WebView using our layout XML file above and then get a reference to this view, set JScript to enabled and load a URL. It really couldn’t be simpler and as before this runs on the device, however there are issues.
Clicking links in WebView above causes the loss of client control over the page. The result is that Flash works, but we cannot control the user experience.
The next step was to attempt to bring the control over loading new pages within the webview instance. To do this we need to extend the WebViewClient class and override the shouldOverrideUrlLoading method, this gives us the opportunity to load the page ourselves; before the browser gets to it.
It looks like this and note that return true after view.loadUrl(url) means don’t bubble this event to other web views:
The result of this is work is that we can simply replace the currently loaded page without launching a new activity, but it comes with a penalty. As when I implemented the custom WebViewClient it became clear that the Flash plugin was not being loaded. Worse still, there’s no way to load it (despite APIs to the contrary) and that’s by design at this time.
So there you have it, the investigation into creating a Flash Application that lives in the browser can only succeed when using the native browser. The Android Platform allows for the creation of custom browser clients and chrome, but the penalty for this is that you loose access to plugins.
Hopefully this has been an informative post, and maybe inspired you to have a look at Android with Flash Player support. Maybe you can extend my examples, or by all means provide some secret code that can help us start up the plugins