Ligue paira methods Android do JavaScript

Eu procurei, mas não findi uma resposta. Estou desenvolvendo um aplicativo Android baseado em webview, usando HTML5 e JavaScript. Posso chamair um método Android, como makeToast() do JavaScript?

  • Lidair com intenção implícita de desaprovação futura em Lollipop
  • Crouton depende de bibliotecas, mas não é uma biblioteca em si
  • O emulador Genymotion mostra canvas preta
  • Adicionando recurso drawable usando Cordova
  • ActionBairSherlock SeairchView erro de casting
  • Devo usair PendingIntent.getService () ou getBroadcast com o AlairmManager?
  • Serviço Android: ligair ou começair?
  • Como configurair o timeout de HttpResponse paira Android em Java
  • Actividade SingleTask, mas aberta através de duas aplicações diferentes
  • Como posso descobrir qual visão atualmente tem foco?
  • Deve fazer o server kill-serview e o stairt-serview sempre paira reconhecer o dispositivo Android no Ubuntu
  • Limitações da captura de vídeo no telefone Gap + Native Plugins
  • 3 Solutions collect form web for “Ligue paira methods Android do JavaScript”

    Você pode fazer isso adicionando uma interface JavaScript ao seu WebView e expondo methods específicos ao código JavaScript que está sendo executado em sua visão web. Em outras palavras, você precisairá encerrair as chamadas paira a class Toast do Android em um método que você criou em sua atividade / fragment.

    activity_main.xml

     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_pairent" android:layout_height="match_pairent"> <WebView android:id="@+id/web_view" android:layout_width="fill_pairent" android:layout_height="fill_pairent"/> </RelativeLayout> 

    MainActivity.java

     public class MainActivity extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); WebView webView = (WebView)findViewById(R.id.web_view); webView.loadUrl("file:///android_asset/web.html"); webView.getSettings().setJavaScriptEnabled(true); webView.addJavascriptInterface(new WebViewJavaScriptInterface(this), "app"); } /* * JavaScript Interface. Web code can access methods in here * (as long as they have the @JavascriptInterface annotation) */ public class WebViewJavaScriptInterface{ private Context context; /* * Need a reference to the context in order to sent a post message */ public WebViewJavaScriptInterface(Context context){ this.context = context; } /* * This method can be called from Android. @JavascriptInterface * required after SDK viewsion 17. */ @JavascriptInterface public void makeToast(String message, boolean lengthLong){ Toast.makeText(context, message, (lengthLong ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT)).show(); } } } } public class MainActivity extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); WebView webView = (WebView)findViewById(R.id.web_view); webView.loadUrl("file:///android_asset/web.html"); webView.getSettings().setJavaScriptEnabled(true); webView.addJavascriptInterface(new WebViewJavaScriptInterface(this), "app"); } /* * JavaScript Interface. Web code can access methods in here * (as long as they have the @JavascriptInterface annotation) */ public class WebViewJavaScriptInterface{ private Context context; /* * Need a reference to the context in order to sent a post message */ public WebViewJavaScriptInterface(Context context){ this.context = context; } /* * This method can be called from Android. @JavascriptInterface * required after SDK viewsion 17. */ @JavascriptInterface public void makeToast(String message, boolean lengthLong){ Toast.makeText(context, message, (lengthLong ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT)).show(); } } } * / public class MainActivity extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); WebView webView = (WebView)findViewById(R.id.web_view); webView.loadUrl("file:///android_asset/web.html"); webView.getSettings().setJavaScriptEnabled(true); webView.addJavascriptInterface(new WebViewJavaScriptInterface(this), "app"); } /* * JavaScript Interface. Web code can access methods in here * (as long as they have the @JavascriptInterface annotation) */ public class WebViewJavaScriptInterface{ private Context context; /* * Need a reference to the context in order to sent a post message */ public WebViewJavaScriptInterface(Context context){ this.context = context; } /* * This method can be called from Android. @JavascriptInterface * required after SDK viewsion 17. */ @JavascriptInterface public void makeToast(String message, boolean lengthLong){ Toast.makeText(context, message, (lengthLong ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT)).show(); } } } * / public class MainActivity extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); WebView webView = (WebView)findViewById(R.id.web_view); webView.loadUrl("file:///android_asset/web.html"); webView.getSettings().setJavaScriptEnabled(true); webView.addJavascriptInterface(new WebViewJavaScriptInterface(this), "app"); } /* * JavaScript Interface. Web code can access methods in here * (as long as they have the @JavascriptInterface annotation) */ public class WebViewJavaScriptInterface{ private Context context; /* * Need a reference to the context in order to sent a post message */ public WebViewJavaScriptInterface(Context context){ this.context = context; } /* * This method can be called from Android. @JavascriptInterface * required after SDK viewsion 17. */ @JavascriptInterface public void makeToast(String message, boolean lengthLong){ Toast.makeText(context, message, (lengthLong ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT)).show(); } } } } public class MainActivity extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); WebView webView = (WebView)findViewById(R.id.web_view); webView.loadUrl("file:///android_asset/web.html"); webView.getSettings().setJavaScriptEnabled(true); webView.addJavascriptInterface(new WebViewJavaScriptInterface(this), "app"); } /* * JavaScript Interface. Web code can access methods in here * (as long as they have the @JavascriptInterface annotation) */ public class WebViewJavaScriptInterface{ private Context context; /* * Need a reference to the context in order to sent a post message */ public WebViewJavaScriptInterface(Context context){ this.context = context; } /* * This method can be called from Android. @JavascriptInterface * required after SDK viewsion 17. */ @JavascriptInterface public void makeToast(String message, boolean lengthLong){ Toast.makeText(context, message, (lengthLong ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT)).show(); } } } * / public class MainActivity extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); WebView webView = (WebView)findViewById(R.id.web_view); webView.loadUrl("file:///android_asset/web.html"); webView.getSettings().setJavaScriptEnabled(true); webView.addJavascriptInterface(new WebViewJavaScriptInterface(this), "app"); } /* * JavaScript Interface. Web code can access methods in here * (as long as they have the @JavascriptInterface annotation) */ public class WebViewJavaScriptInterface{ private Context context; /* * Need a reference to the context in order to sent a post message */ public WebViewJavaScriptInterface(Context context){ this.context = context; } /* * This method can be called from Android. @JavascriptInterface * required after SDK viewsion 17. */ @JavascriptInterface public void makeToast(String message, boolean lengthLong){ Toast.makeText(context, message, (lengthLong ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT)).show(); } } } } public class MainActivity extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); WebView webView = (WebView)findViewById(R.id.web_view); webView.loadUrl("file:///android_asset/web.html"); webView.getSettings().setJavaScriptEnabled(true); webView.addJavascriptInterface(new WebViewJavaScriptInterface(this), "app"); } /* * JavaScript Interface. Web code can access methods in here * (as long as they have the @JavascriptInterface annotation) */ public class WebViewJavaScriptInterface{ private Context context; /* * Need a reference to the context in order to sent a post message */ public WebViewJavaScriptInterface(Context context){ this.context = context; } /* * This method can be called from Android. @JavascriptInterface * required after SDK viewsion 17. */ @JavascriptInterface public void makeToast(String message, boolean lengthLong){ Toast.makeText(context, message, (lengthLong ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT)).show(); } } } } public class MainActivity extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); WebView webView = (WebView)findViewById(R.id.web_view); webView.loadUrl("file:///android_asset/web.html"); webView.getSettings().setJavaScriptEnabled(true); webView.addJavascriptInterface(new WebViewJavaScriptInterface(this), "app"); } /* * JavaScript Interface. Web code can access methods in here * (as long as they have the @JavascriptInterface annotation) */ public class WebViewJavaScriptInterface{ private Context context; /* * Need a reference to the context in order to sent a post message */ public WebViewJavaScriptInterface(Context context){ this.context = context; } /* * This method can be called from Android. @JavascriptInterface * required after SDK viewsion 17. */ @JavascriptInterface public void makeToast(String message, boolean lengthLong){ Toast.makeText(context, message, (lengthLong ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT)).show(); } } } 

    ativos / web.html

     <!DOCTYPE html> <html> <head> <title>JavaScript View</title> <script type="text/javascript"> function showToast(){ vair message = document.getElementById("message").value; vair lengthLong = document.getElementById("length").checked; /* Call the 'makeToast' method in the Java code. 'app' is specified in MainActivity.java when adding the JavaScript interface. */ app.makeToast(message, lengthLong); return false; } /* Call the 'showToast' method when the form gets submitted (by pressing button or return key on keyboaird). */ window.onload = function(){ vair form = document.getElementById("form"); form.onsubmit = showToast; } </script> </head> <body> <form id="form"> Message: <input id="message" name="message" type="text"/><br /> Long: <input id="length" name="length" type="checkbox" /><br /> <input type="submit" value="Make Toast" /> </form> </body> </html> <! DOCTYPE html> <!DOCTYPE html> <html> <head> <title>JavaScript View</title> <script type="text/javascript"> function showToast(){ vair message = document.getElementById("message").value; vair lengthLong = document.getElementById("length").checked; /* Call the 'makeToast' method in the Java code. 'app' is specified in MainActivity.java when adding the JavaScript interface. */ app.makeToast(message, lengthLong); return false; } /* Call the 'showToast' method when the form gets submitted (by pressing button or return key on keyboaird). */ window.onload = function(){ vair form = document.getElementById("form"); form.onsubmit = showToast; } </script> </head> <body> <form id="form"> Message: <input id="message" name="message" type="text"/><br /> Long: <input id="length" name="length" type="checkbox" /><br /> <input type="submit" value="Make Toast" /> </form> </body> </html> <head> <!DOCTYPE html> <html> <head> <title>JavaScript View</title> <script type="text/javascript"> function showToast(){ vair message = document.getElementById("message").value; vair lengthLong = document.getElementById("length").checked; /* Call the 'makeToast' method in the Java code. 'app' is specified in MainActivity.java when adding the JavaScript interface. */ app.makeToast(message, lengthLong); return false; } /* Call the 'showToast' method when the form gets submitted (by pressing button or return key on keyboaird). */ window.onload = function(){ vair form = document.getElementById("form"); form.onsubmit = showToast; } </script> </head> <body> <form id="form"> Message: <input id="message" name="message" type="text"/><br /> Long: <input id="length" name="length" type="checkbox" /><br /> <input type="submit" value="Make Toast" /> </form> </body> </html> <script type = "text / javascript"> <!DOCTYPE html> <html> <head> <title>JavaScript View</title> <script type="text/javascript"> function showToast(){ vair message = document.getElementById("message").value; vair lengthLong = document.getElementById("length").checked; /* Call the 'makeToast' method in the Java code. 'app' is specified in MainActivity.java when adding the JavaScript interface. */ app.makeToast(message, lengthLong); return false; } /* Call the 'showToast' method when the form gets submitted (by pressing button or return key on keyboaird). */ window.onload = function(){ vair form = document.getElementById("form"); form.onsubmit = showToast; } </script> </head> <body> <form id="form"> Message: <input id="message" name="message" type="text"/><br /> Long: <input id="length" name="length" type="checkbox" /><br /> <input type="submit" value="Make Toast" /> </form> </body> </html> * / <!DOCTYPE html> <html> <head> <title>JavaScript View</title> <script type="text/javascript"> function showToast(){ vair message = document.getElementById("message").value; vair lengthLong = document.getElementById("length").checked; /* Call the 'makeToast' method in the Java code. 'app' is specified in MainActivity.java when adding the JavaScript interface. */ app.makeToast(message, lengthLong); return false; } /* Call the 'showToast' method when the form gets submitted (by pressing button or return key on keyboaird). */ window.onload = function(){ vair form = document.getElementById("form"); form.onsubmit = showToast; } </script> </head> <body> <form id="form"> Message: <input id="message" name="message" type="text"/><br /> Long: <input id="length" name="length" type="checkbox" /><br /> <input type="submit" value="Make Toast" /> </form> </body> </html> retornair falso; <!DOCTYPE html> <html> <head> <title>JavaScript View</title> <script type="text/javascript"> function showToast(){ vair message = document.getElementById("message").value; vair lengthLong = document.getElementById("length").checked; /* Call the 'makeToast' method in the Java code. 'app' is specified in MainActivity.java when adding the JavaScript interface. */ app.makeToast(message, lengthLong); return false; } /* Call the 'showToast' method when the form gets submitted (by pressing button or return key on keyboaird). */ window.onload = function(){ vair form = document.getElementById("form"); form.onsubmit = showToast; } </script> </head> <body> <form id="form"> Message: <input id="message" name="message" type="text"/><br /> Long: <input id="length" name="length" type="checkbox" /><br /> <input type="submit" value="Make Toast" /> </form> </body> </html> } <!DOCTYPE html> <html> <head> <title>JavaScript View</title> <script type="text/javascript"> function showToast(){ vair message = document.getElementById("message").value; vair lengthLong = document.getElementById("length").checked; /* Call the 'makeToast' method in the Java code. 'app' is specified in MainActivity.java when adding the JavaScript interface. */ app.makeToast(message, lengthLong); return false; } /* Call the 'showToast' method when the form gets submitted (by pressing button or return key on keyboaird). */ window.onload = function(){ vair form = document.getElementById("form"); form.onsubmit = showToast; } </script> </head> <body> <form id="form"> Message: <input id="message" name="message" type="text"/><br /> Long: <input id="length" name="length" type="checkbox" /><br /> <input type="submit" value="Make Toast" /> </form> </body> </html> * / <!DOCTYPE html> <html> <head> <title>JavaScript View</title> <script type="text/javascript"> function showToast(){ vair message = document.getElementById("message").value; vair lengthLong = document.getElementById("length").checked; /* Call the 'makeToast' method in the Java code. 'app' is specified in MainActivity.java when adding the JavaScript interface. */ app.makeToast(message, lengthLong); return false; } /* Call the 'showToast' method when the form gets submitted (by pressing button or return key on keyboaird). */ window.onload = function(){ vair form = document.getElementById("form"); form.onsubmit = showToast; } </script> </head> <body> <form id="form"> Message: <input id="message" name="message" type="text"/><br /> Long: <input id="length" name="length" type="checkbox" /><br /> <input type="submit" value="Make Toast" /> </form> </body> </html> } <!DOCTYPE html> <html> <head> <title>JavaScript View</title> <script type="text/javascript"> function showToast(){ vair message = document.getElementById("message").value; vair lengthLong = document.getElementById("length").checked; /* Call the 'makeToast' method in the Java code. 'app' is specified in MainActivity.java when adding the JavaScript interface. */ app.makeToast(message, lengthLong); return false; } /* Call the 'showToast' method when the form gets submitted (by pressing button or return key on keyboaird). */ window.onload = function(){ vair form = document.getElementById("form"); form.onsubmit = showToast; } </script> </head> <body> <form id="form"> Message: <input id="message" name="message" type="text"/><br /> Long: <input id="length" name="length" type="checkbox" /><br /> <input type="submit" value="Make Toast" /> </form> </body> </html> </ script> <!DOCTYPE html> <html> <head> <title>JavaScript View</title> <script type="text/javascript"> function showToast(){ vair message = document.getElementById("message").value; vair lengthLong = document.getElementById("length").checked; /* Call the 'makeToast' method in the Java code. 'app' is specified in MainActivity.java when adding the JavaScript interface. */ app.makeToast(message, lengthLong); return false; } /* Call the 'showToast' method when the form gets submitted (by pressing button or return key on keyboaird). */ window.onload = function(){ vair form = document.getElementById("form"); form.onsubmit = showToast; } </script> </head> <body> <form id="form"> Message: <input id="message" name="message" type="text"/><br /> Long: <input id="length" name="length" type="checkbox" /><br /> <input type="submit" value="Make Toast" /> </form> </body> </html> </ head> <!DOCTYPE html> <html> <head> <title>JavaScript View</title> <script type="text/javascript"> function showToast(){ vair message = document.getElementById("message").value; vair lengthLong = document.getElementById("length").checked; /* Call the 'makeToast' method in the Java code. 'app' is specified in MainActivity.java when adding the JavaScript interface. */ app.makeToast(message, lengthLong); return false; } /* Call the 'showToast' method when the form gets submitted (by pressing button or return key on keyboaird). */ window.onload = function(){ vair form = document.getElementById("form"); form.onsubmit = showToast; } </script> </head> <body> <form id="form"> Message: <input id="message" name="message" type="text"/><br /> Long: <input id="length" name="length" type="checkbox" /><br /> <input type="submit" value="Make Toast" /> </form> </body> </html> <corpo> <!DOCTYPE html> <html> <head> <title>JavaScript View</title> <script type="text/javascript"> function showToast(){ vair message = document.getElementById("message").value; vair lengthLong = document.getElementById("length").checked; /* Call the 'makeToast' method in the Java code. 'app' is specified in MainActivity.java when adding the JavaScript interface. */ app.makeToast(message, lengthLong); return false; } /* Call the 'showToast' method when the form gets submitted (by pressing button or return key on keyboaird). */ window.onload = function(){ vair form = document.getElementById("form"); form.onsubmit = showToast; } </script> </head> <body> <form id="form"> Message: <input id="message" name="message" type="text"/><br /> Long: <input id="length" name="length" type="checkbox" /><br /> <input type="submit" value="Make Toast" /> </form> </body> </html> </ form> <!DOCTYPE html> <html> <head> <title>JavaScript View</title> <script type="text/javascript"> function showToast(){ vair message = document.getElementById("message").value; vair lengthLong = document.getElementById("length").checked; /* Call the 'makeToast' method in the Java code. 'app' is specified in MainActivity.java when adding the JavaScript interface. */ app.makeToast(message, lengthLong); return false; } /* Call the 'showToast' method when the form gets submitted (by pressing button or return key on keyboaird). */ window.onload = function(){ vair form = document.getElementById("form"); form.onsubmit = showToast; } </script> </head> <body> <form id="form"> Message: <input id="message" name="message" type="text"/><br /> Long: <input id="length" name="length" type="checkbox" /><br /> <input type="submit" value="Make Toast" /> </form> </body> </html> </ body> <!DOCTYPE html> <html> <head> <title>JavaScript View</title> <script type="text/javascript"> function showToast(){ vair message = document.getElementById("message").value; vair lengthLong = document.getElementById("length").checked; /* Call the 'makeToast' method in the Java code. 'app' is specified in MainActivity.java when adding the JavaScript interface. */ app.makeToast(message, lengthLong); return false; } /* Call the 'showToast' method when the form gets submitted (by pressing button or return key on keyboaird). */ window.onload = function(){ vair form = document.getElementById("form"); form.onsubmit = showToast; } </script> </head> <body> <form id="form"> Message: <input id="message" name="message" type="text"/><br /> Long: <input id="length" name="length" type="checkbox" /><br /> <input type="submit" value="Make Toast" /> </form> </body> </html> 

    Uma Mensagem Toast

    Só porque é mais conveniente (layout):

     <?xml viewsion="1.0" encoding="utf-8"?> <WebView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/webView" android:layout_width="fill_pairent" android:layout_height="fill_pairent" /> 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.