[英]calling activity from webview with @JavascriptInterface
I'm stuck on my project and requesting your help!我被困在我的项目上并请求您的帮助!
I'm trying to call an Activity(BarcodeScannerActivity.java) from webview in MainActivity.java with button click using javascript call.我正在尝试使用 javascript 调用通过单击按钮从 MainActivity.java 中的 webview 调用 Activity(BarcodeScannerActivity.java)。 I think I messed up with parameters somewhere... I'm not sure where I've made mistake, I just copied and modified the codes from blogs and stackoverflow.
我想我在某处搞砸了参数......我不确定我在哪里犯了错误,我只是从博客和stackoverflow中复制并修改了代码。
I'm having this error in my Android Studio logcat:我的 Android Studio logcat 中出现此错误:
[INFO:CONSOLE(315)] "Uncaught Error: Error calling method on NPObject.", source: http:// .........
[信息:控制台(315)]“未捕获的错误:在 NPObject 上调用方法时出错。”,来源:http://........
this is my server side html:这是我的服务器端 html:
<input type="button" value="Scanner" id="BtnScan" class="btn_dark" onclick="callActivity();" />
<script>
function callActivity() {
Android.openBarcodeScanner();
}
</script>
this is my MainActivity.java containing webview:这是我的 MainActivity.java 包含 webview:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
//getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
//getActionBar().hide();
setContentView(R.layout.activity_main);
MainWebView = (WebView) findViewById(R.id.mainWebview);
MainWebView.setWebViewClient(new WebViewClient());
//-- add javascript listener
MainWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
MainWebView.getSettings().setJavaScriptEnabled(true);
MainWebView.loadUrl(WebViewURL);
}
..skipped for brevity..
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
@JavascriptInterface
public void openBarcodeScanner(String str) {
Intent barcodeScanIntent = new Intent(mContext, BarcodeScannerActivity.class);
mContext.startActivity(barcodeScanIntent);
}
}
and last, the BarcodeScannerActivity.java that wants to be called:最后,要调用的 BarcodeScannerActivity.java:
public class BarcodeScannerActivity extends Activity implements View.OnClickListener {
// private static final R = ;
// use a compound button so either checkbox or switch widgets work.
private CompoundButton autoFocus;
private CompoundButton useFlash;
private TextView statusMessage;
private TextView barcodeValue;
private static final int RC_BARCODE_CAPTURE = 9001;
private static final String TAG = "BarcodeMain";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.barcode_scanner);
statusMessage = (TextView)findViewById(R.id.status_message);
barcodeValue = (TextView)findViewById(R.id.barcode_value);
autoFocus = (CompoundButton) findViewById(R.id.auto_focus);
useFlash = (CompoundButton) findViewById(R.id.use_flash);
findViewById(R.id.read_barcode).setOnClickListener(this);
}
/**
* Called when a view has been clicked.
*
* @param v The view that was clicked.
*/
@Override
public void onClick(View v) {
if (v.getId() == R.id.read_barcode) {
// launch barcode activity.
Intent intent = new Intent(this, BarcodeCaptureActivity.class);
intent.putExtra(BarcodeCaptureActivity.AutoFocus, autoFocus.isChecked());
intent.putExtra(BarcodeCaptureActivity.UseFlash, useFlash.isChecked());
startActivityForResult(intent, RC_BARCODE_CAPTURE);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RC_BARCODE_CAPTURE) {
if (resultCode == CommonStatusCodes.SUCCESS) {
if (data != null) {
Barcode barcode = data.getParcelableExtra(BarcodeCaptureActivity.BarcodeObject);
statusMessage.setText(R.string.barcode_success);
barcodeValue.setText(barcode.displayValue);
Log.d(TAG, "Barcode read: " + barcode.displayValue);
} else {
statusMessage.setText(R.string.barcode_failure);
Log.d(TAG, "No barcode captured, intent data is null");
}
} else {
statusMessage.setText(String.format(getString(R.string.barcode_error),
CommonStatusCodes.getStatusCodeString(resultCode)));
}
}
else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
and one last question is, would this javascript code work in my situation, too?最后一个问题是,这段 javascript 代码是否也适用于我的情况? or do I need to modify a bit?
还是我需要修改一下?
function callActivity(){
if(window.android){
console.info("android");
window.android.openBarcodeScanner(number.value);
}
else{
console.info("web");
document.form.submit();
}
}
}
Thank you so much in advance!非常感谢您!
Your interface takes a string as a parameter:您的界面将字符串作为参数:
@JavascriptInterface
public void openBarcodeScanner(String str) {
But your invocation doesn't pass any parameters:但是您的调用不传递任何参数:
Android.openBarcodeScanner();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.