My Android app objective is to scan a bar/QR code and pass it to the HTML file loaded at webView element.
The app has ONE layout only, that contain a button and the webview element. Once the button is clicked, the app open the bar/QR scanner and return the result to the activity, and I need to pass this result to my HTML file for further processing.
The MainActivity is working correctly, as in this tutorial, and return the result here:
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanningResult != null) {
String scanContent = scanningResult.getContents();
String scanFormat = scanningResult.getFormatName();
formatTxt.setText("FORMAT: " + scanFormat);
contentTxt.setText("CONTENT: " + scanContent);
/*** I NEED to SEND the 'scanContent' to the my HTML file loaded at the WebView element ***/
}
else{
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
I read this and this and this and this and thisand got the below below files:
JavaScriptInterface.java
public class JavaScriptInterface {
Context mContext;
/** Instantiate the interface and set the context */
JavaScriptInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
@JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}
and the WebViewActivity.java:
public class WebViewActivity extends Activity {
// private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = (WebView)findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
/*** I think something wrong in this area **/
webView.loadUrl("file:///android_asset/myFile.html");
webView.setWebViewClient(new WebViewClient(){
public void onPageFinished(WebView view, String url){
view.loadUrl("javascript:init('" + url + "')");
}
});
//webView.loadUrl("http://www.google.com");
// String customHtml = "<html><body>" +
// ""+
// "</body></html>";
// webView.loadData(customHtml, "text/html", "UTF-8");
}
}
No errors appeared in the Android Studio IDE, and when running the app the button for scanning work, but nothing appear in the webview, only white page!! white page always, from the start up of the up, and after scanning the code!
Any idea where I got lost, and how to fix it!
Note I need to send the data to the JavaScript used in the HTML file loaded at webView in the same activity, not just sending URL to be loaded.


