0

I've the following code in my jsp file:

 <script>
 var linkto = magicnumber.devuelveMagicNumber();
 document.writeln("<a href='"+linkto+".com'>"+linkto+"</a>");
 </script>

On Clase.java I've declared the following class:

public class MagicNumber
{
    Clase padre;
      public MagicNumber(Clase padre)
      {
        this.padre=padre;
    }

    @JavascriptInterface
    public String devuelveMagicNumber()
    {
        return padre.devuelveMagicNumber();         
    }
}

And in the class Clase of Class.java

public String devuelveMagicNumber()
{
    return magicnumber;
}

But when I load the page on my app it doesn't show anything where it should show the link, it should be showing a magic number that my android app has generated.

What I find pretty strange is that the following code do works:

On my jsp file:

<a href="javascript:void();"    onclick="scanner.doScan('<%=Utils.parseString(request,"accion")%>');return false;">Tengo tarjeta &gt;</a>

And on my Clase.java I've defined the following class

 public class Scanner
{
    Clase padre;
    public Scanner(Clase padre)
    {
        this.padre=padre;
    }

    @JavascriptInterface
    public void doScan(String accion)
    {
        padre.doScan(accion);           
    }           

}

And finally the following is in the Class Clase in Clase.java.

public void doScan(String accion)
{
 this.accion=accion;
    Intent intent = new Intent("com.google.zxing.client.android.SCAN");
    try
    {
        intent.putExtra("SCAN_MODE","QR_CODE_MODE");
        startActivityForResult(intent,0);
    }
    catch(Exception e)
    {       Toast.makeText(this,R.string.error_app_qr,Toast.LENGTH_LONG).show();
    }
}

I don't have a clue why the second one works and the first one doesn't... Could anyone please guide me about what could I be missing?

Thanks for your attention.

user2638180
  • 1,013
  • 16
  • 37
  • Are you setting the javascript interface? There are many useful examples here on SO, like this one http://stackoverflow.com/questions/10472839/using-javascript-in-android-webview – SimonSays Feb 25 '14 at 00:04
  • You are right, I had forgotten to set the javascript interface. Thanks for your tip! – user2638180 Feb 25 '14 at 09:57

0 Answers0