I have HTML page like Geotagger where I can Search in the list using input text,
But I want to make a button where I can use the zxing barcode.
so I can easily find what I want from the list.
how can I make Intent in the javascript?
can anybody help me?
hello @W_Brunette
I did use the odkcommon.js to launch an intent but I can not receive data back .
window.addEventListener(“load”, function () {
odkCommon.registerListener(
function () {
alert(“inside the listener”);
var action = odkCommon.viewFirstQueuedAction();
$(“#searchForText”).val(“inside the listener”);
if (action !== null) {
console.debug(action);
$(“#searchForText”).val(JSON.stringify(action));
var dispatchStruct = action.dispatchStruct;
if (dispatchStruct && dispatchStruct.userAction == “scan-kast-code”) {
var jsonValue = action.jsonValue;
$(“#searchForText”).val(“status = " + jsonValue.status);
if (jsonValue.status === 0) {
$(”#searchForText").val(jsonValue.result);
console.log(jsonValue);
}
}
// process action – be idempotent!
// if processing fails, the action will still
// be on the queue.
odkCommon.removeFirstQueuedAction();
}
});
$(“#barcode”).click(function () {
var dispatchStruct = { userAction: “scan-kast-code” };
var intentArgs = {
// extras: extrasBundle,
// uri: // set the data field of intent to this
// data: // unless data is supplied – that takes precedence
// type: // set the intent type to this value
// package: // set the intent package to this value
};
Hello,
Are you just trying to get the value of the scan?
If so, I believe something like this can do it?
Create a function to handle the call back and give a name to your userAction (in this example we’ll call it “thatsScanny”) like e.g.
function javaChange() {
var action = odkCommon.viewFirstQueuedAction();
if ( action !== null ) {
console.log("call back called");
if (action.dispatchStruct.userAction == "thatsScanny") {
odkCommon.removeFirstQueuedAction();
if (action.jsonValue.result && action.jsonValue.result.SCAN_RESULT) {
var scanRes = action.jsonValue.result.SCAN_RESULT;
// call you function to handle the code.. e.g doSearch()
doSearch(scanRes);
}
}
}
}
Hook it up in the pages initialization method,e.g.
function display() { // we will call this onLoad
console.log("Setting up display");
odkCommon.registerListener( javaChange );
javaChange(); // invoke it once, just because...
// Assuming you have a button with id = btnScan
$("#btnScan").on("click", function() {
var dispatchStruct = {userAction: 'thatsScanny'};
var x = odkSurvey.scanBarcode(dispatchStruct);
console.log(x);
})
}
Thanks for your quick reply, @Emil. It seems that your suggestion is very similar to my code. I cannot see the difference and why my code does not work. The problem is the registered listener never get called after odkCommonIf.doAction() is finished.
It does not work for me as odkSurvey.js in turn also calls OdkCommon.doAction().
I tried debugging ODK Table app with Android Studio and found why the listener never gets called. I will list my findings in another topic.
Anyway, I could make it work with a workaround. Thanks for following up.
@W_Brunette, please correct me if I am wrong. As my understanding, once doAction() is finished, the Android app will call this method in ODKWebView to signal its client to call the registered listener: ODKWebView.class:
this.isLoadPageFrameworkFinished is controlling whether the WebView should sends signal to client.
The problem I have found is: Once the barcode scanner app is launched, the current view is paused which then set this.isLoadPageFrameworkFinished to false. However, it is never set back to true then (e.g after the barcode scanner app exits and the view is resumed)
public void onPause() {
...
this.isLoadPageFrameworkFinished = false;
}
That’s what I have observed through app debugging. Any thoughts?
@Emil It does not work for me and I do not really understand why it works for you. As my understanding, when ODK Table cannot send signal for its client to fetch the result of doAction() (your javaChange() call), the result will then be retained in guardedQueuedActions list in ODK Table. Your next call of javaChange() will actually fetch that result and remove the result from the ODK. That means, nth call of listener will fetch result of (n-1)th doAction()?
Hi, this might be coming a bit late but I noticed you registered the listener and called it immediately even before calling the odkCommon.doAction. The goal is to register the listener on page load and call it immediately after registering it because when the button to scan is clicked, control leaves the app and it is later returned to the activity that launched the intent - reason why it is good to register the listener and call it at once on page load.
I ran into this post because I faced similar issues.