Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling javascript function from java in Codenameone

**Update i clarified the question ** I am working on a Codenameone app with a local webpage. The webpage is a local html file and a bundle of javascript files. The webpage is loaded in the webbrowsercomponent (bc). i like to call this javascript functions directly from codenameone. I first used bc.postmessage. But i noticed that the message queue is not disposed after sending the message. I am now looking at the bc.execute method in Codenameone.But i can't figure out with codenameone documentation how i must do that.

i have in javascript the function setLocation(dat). With that function i populate a htmltable with location data.

How do i sent the information to the function from within java with bc.execute. What is the proper code? bc.execute("setgpsdata( ${0} }), result); is wrong. i got a error message in netbeans ; no suitable method for execute

public void locationUpdated(Location loc) {
         
         locationReady=true;
                try {   
                      
                 loc = Im.getCurrentLocation();
            
        
                 float Direction =  loc.getDirection();
 
                  float acc =   loc.getAccuracy();
                  double latp = loc.getLatitude();
                  double lonp = loc.getLongitude();
                  double speed = loc.getVelocity();
                  
           
        
    
          double speedo = 3.6 * speed;
          double roundspd = MathUtil.round(speedo *10);
          roundspd = roundspd/10;

          
  
          String result = cde+ String.valueOf( roundspd) + ","+ String.valueOf(Direction) + ","+ String.valueOf(acc) + ","+ String.valueOf(latp) + "," +String.valueOf(lonp); 


          
          
         //  bc.postMessage(result.toString(), "*");;   
                  
            String js = "setgpsdata( ${0} })"; 

            bc.execute("setgpsdata( ${0} }), result);
             
                } catch (IOException e) {
             
                   Dialog.show("GPS error4", "Your location could not be found, please try going outside for a better GPS signal", "Ok", null);
                }
            
             

        }

On the javascript side:

function setgpsdata(dat) {
var dat = [];
dat = data.split(",");
var speed = dat[0];
var heading = dat[1];
var acc = dat[2];
var latwp = dat[3];
var lonwp = dat[4];


var myTable2 = document.getElementById("GPSdata");
myTable2.rows[0].cells[1].innerHTML = speed + ' [km/h]';
myTable2.rows[1].cells[1].innerHTML = heading;
myTable2.rows[2].cells[1].innerHTML = acc + ' [m]';
myTable2.rows[3].cells[1].innerHTML = latTostring(latwp);
myTable2.rows[4].cells[1].innerHTML = lonTostring(lonwp);

}

like image 561
user3025401 Avatar asked Mar 09 '26 18:03

user3025401


1 Answers

Pass String result directly to bc execute function

bc.execute("setgpsdata('" + result + "')");

See this

https://github.com/Eric-Chomba/

BrowserComponent Demo

See also in "JavaScript Interaction" menu of this app

FeaturesCN

like image 197
Eric Avatar answered Mar 11 '26 09:03

Eric