1

I mean without cordova or other framework. I'm pretty sure i need to write Java code and link it somehow with html5 through the android webview. If it is possible, can get a little example how to connect to the camera or other sensor.

Dany Maor
  • 2,391
  • 2
  • 18
  • 26
  • Could you please look into below article.It might help u. http://stackoverflow.com/questions/4474508/access-accelerometer-via-javascript-in-android – Umakanta Behera Sep 21 '16 at 04:01
  • What are your "red lines", if you already accept to do it android java, and output in a webview, then there is no problem, simple. Launch, well that's different (I assume you know what the virtual machine, and a sandbox is)[and why they are there], seems arbitrary. You should update your tags, add android at least. – Jon Goodwin Sep 25 '16 at 00:43

2 Answers2

1

Some of the sensors have a JavaScript API such as geolocation, orientation (gyroscope) and the battery. To access the camera you could use MediaDevices.getUserMedia, however, this is still in an experimental stage and is not supported by all Android devices. For more information refer to this link.

0

Look into JavascriptInterface

https://developer.android.com/reference/android/webkit/WebView.html https://developer.android.com/guide/webapps/webview.html

Specifically, addJavascriptInterface(java.lang.Object, java.lang.String))

@JavascriptInterface
class JsInterface {
  public void startCamera() { ... }
}

WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.addJavascriptInterface(new JsInterface(), "androidInterface");

Basically, add the JavascriptInterface, and enable javascript on the web view. Then in your javascript you can detect if the interface exists like so:

if ("undefined" != typeof androidInterface) {
    androidInterface.startCamera();
}

Now in the Java code for startCamera, you can do whatever native stuff you need done.

Michael
  • 430
  • 6
  • 20