我正在尝试使用jsinterface和zxing在我的android中获取扫描仪。 对于Java native,这是可行的, 但我不会尝试使用jsinterface在webview中运行Toast并显示, 但扫描仪无法运行。 我该怎么办.. 我的代码如下所示
//my Activity
public class WebViewActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
WebView webView = (WebView) findViewById(R.id.webview);
webView.loadUrl("file:///android_asset/index.html");
//interface for show toast
webView.addJavascriptInterface(new WebAppInterface(this), "AndroidInterface");
//interface for show scanner
webView.addJavascriptInterface(new XzingInterface(this), "Android");
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(new MyWebViewClient());
webView.setWebChromeClient(new MyWebChromeClient());
}
}
//class XzingInterface
public void ScanNow() {
integrator = new IntentIntegrator(this);
integrator.initiateScan();
}
//class WebAppInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
//HTML
<button ocnlclik="Scan()">Scan</button> //not respond anything
<button ocnlclik="showAlert('Test Alert JSinterface')">Test Alert</button> //alert is run perfect
<script type="text/javascript">
function Scan(){
Android.ScanNow();
}
function showAlert(msg){
AndroidInterface.showToast(toast);
}
</script>