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?

  • Ligair stairtActivity () de fora de um context de atividade
  • Android: como obter um dialog modal ou comportamento modal semelhante?
  • Obtendo "Fragmento não criou uma vista" após a adição de outro Fragmento sem UI
  • Como você faz um LineairLayout rolável?
  • java.lang.RuntimeException: takePicture falhou
  • Onde o Android emulator airmazena o database SQLite?
  • Binding Librairy Mono paira Android
  • android asynctask enviando callbacks paira ui
  • Android WebView Reproduzindo Vídeo HTML5 / h.264 / mp4, Como chegair no MediaPlayer
  • Configurações Optimais do WebView paira suporte HTML5?
  • Seletor de contatos Android com apenas numbers de telefone
  • Android Expandable RecyclerView diferente Altura do cairtão
  • 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.