从@JavascriptInterface的WebView调用活动 [英] calling activity from webview with @JavascriptInterface
问题描述
我卡在我的项目,并请求您的帮助!结果
我试图从网页视图中MainActivity.java调用活动(酒吧codeScannerActivity.java)用javascript调用按钮的点击。我想我搞砸了某个参数...我不知道,我做了错误,我刚才复制并从博客和修改codeS计算器。
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. 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.
我有在我的Android工作室logcat的这个错误:
I'm having this error in my Android Studio logcat:
[信息:控制台(315)]未捕获的错误:错误调用NPObject方法。出处:http:// .........
[INFO:CONSOLE(315)] "Uncaught Error: Error calling method on NPObject.", source: http:// .........
这是我的服务器端HTML:
this is my server side html:
<input type="button" value="Scanner" id="BtnScan" class="btn_dark" onclick="callActivity();" />
<script>
function callActivity() {
Android.openBarcodeScanner();
}
</script>
这是我含MainActivity.java的WebView:
this is my MainActivity.java containing 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);
}
}
和最后一个想要被称为酒吧codeScannerActivity.java:
and last, the BarcodeScannerActivity.java that wants to be called:
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);
}
}
}
和最后一个问题是,会在我的情况这个JavaScript code可以吗?
或者我需要修改一点?
and one last question is, would this javascript code work in my situation, too? 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();
}
}
}
感谢你这么多提前!
推荐答案
您接口需要一个字符串作为参数:
Your interface takes a string as a parameter:
@JavascriptInterface
public void openBarcodeScanner(String str) {
但是,你的调用不会传递任何参数:
But your invocation doesn't pass any parameters:
Android.openBarcodeScanner();
这篇关于从@JavascriptInterface的WebView调用活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!