How can Javascript access hardware on Android?

Android1While the Apache Cordova project is providing a cross-platform and plug-in solution to deploying web applications as native mobile apps on Android, iPhones and Windows while giving you access to the hardware such as the camera, GPS and storage via its API, it is only able to provide this capability because of native capabilities on the devices.  How is this possible when normally web applications cannot access native hardware APIs?
Android allows you to create or embed web applications inside a native app via a Java class in its SDK called WebView, using its WebKit rendering engine to provide a browser inside your native app.  In this class is a powerful method called

public void addJavascriptInterface (Object object, String name)

that let’s you expose your Java class as a Javascript object in the browser.  This is similar, in many ways, to how you can expose Java to an interpretive language running in Java, such as Jython, where Python can run inside Java, and you can expose your Java application to that Python.  This provides the ability to expose virtually any native Android functionality to your embedded web applications, including capabilities you create in Java.
Spread the love

About Erik S

With a passion for Investing, Business, Technology, Economics, People and God, Erik seeks to impact people's lives before he leaves. Contact Erik
This entry was posted in Android, Mobile and tagged , , , , . Bookmark the permalink.

Leave a Reply