2012-01-25 3 views
0

Chers tous je viens de passer et de récupérer une certaine valeur de javascript et android. Je pourrais pouvoir passer la valeur javascript à android. Mon problème est que je n'ai pas pu retourner la valeur. Ceci est mon extrait. tout organisme peut me aiderPasser et retourner la valeur de javascript à android

HTML et script

 <html> 
    <head> 
    <script src="phonegap-1.3.0.js" type="text/javascript"></script> 
    <script type="text/javascript"> 
     function invoke(param1,param2) 
     { 
      alert('Hai'); 
      //invoking the JavascriptBridge registered under the 'jb' namespace 
      var result = jb.callMe(param1,param2); 

      //doing something with the return value, it should be concatenation 
      //of the two input parameters 
      alert(result); 

     } 
    </script> 
</head> 
<body> 
    <form id = "returning"> 
<h2>Demonstrating Android Javascript-To-Java Bridge</h2> 

<input type="button" value="Invoke Bridge" onclick="invoke('Hello','World');"/> 
    </form> 
</body> 

Android:

public class ReturnAndroidValActivity extends Activity 
    { 
    private WebView webView; 

    public ReturnAndroidValActivity() 
    { 

    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
      super.onCreate(savedInstanceState); 
    } 

    @Override 
    protected void onStart() 
    { 
      super.onStart(); 
    } 

    @Override 
    protected void onResume() 
    { 
      try 
      { 
        super.onResume(); 

        //render the main screen 
        // this.setContentView(ViewHelper.findLayoutId(this, "main")); 

        //Find the WebView control 
        //this.webView = (WebView)ViewHelper.findViewById(this, "webview"); 
        setContentView(R.layout.main); 
        this.webView = (WebView)findViewById(R.id.mybrowser); 



        //Enable Javascript...This is needed so that Javascript is allowed to execute 
        //inside the WebView 
        WebSettings webSettings = this.webView.getSettings(); 
        webSettings.setJavaScriptEnabled(true); 

        //Register the 'Javascript Bridge' class under the 'jb' namespace 
        //this class can be invoked from the HTML/Javascript side 
        this.webView.addJavascriptInterface(new JavascriptBridge(), "jb"); 

        //Register the WebChromeClient to assist with alerts/debugging 
        this.webView.setWebChromeClient(new MyWebChromeClient()); 

        //Load assets/html/index.html resource into the WebView control 
        this.webView.loadUrl("file:///android_asset/www/index.html"); 
      } 
      catch(Exception e) 
      { 
        e.printStackTrace(System.out); 
      } 
    } 

    final class JavascriptBridge 
    { 
      public String callMe(String param1, String param2) 
      { 
        //Generate the returnValue from the bridge 
        String toastValue = param1 + "," + param2; 

        //Setup the Toast 
        Toast toast = Toast.makeText(ReturnAndroidValActivity.this, toastValue, Toast.LENGTH_LONG); 

        //Show the Toast 
        toast.show(); 

        return toastValue; 
      } 
    } 

    /** 
* Provides a hook for calling "alert" from javascript. Useful for 
* debugging your javascript. 
*/ 
final class MyWebChromeClient extends WebChromeClient 
{ 
    @Override 
    public boolean onJsAlert(WebView view, String url, String message, JsResult result) 
    { 
     Log.d("JavascriptBridge", message); 

     resu lt.confirm(); 
     return true; 
    } 




} 

}

+0

avoir répondu à la même question hier. Voir ici: http://stackoverflow.com/q/8982570/1108032 –

+0

@Boris .... Ya ... Mais dans mon cas, plz dire où modifier .... je vais passer par la suite –

Répondre

1

Définir une fonction plus dans le javascript:

<script> 
    function my_callback_function(param){ 
    alert("Called with value: " + param); 
    } 
</script> 

Ensuite, vous appelez cette fonction par le WebView dans le code natif comme ça:

webView.loadUrl("javascript:my_callback_function('TheValue')"); 
Questions connexes