usando javascript na webview do Android

Estou tentando iniciair uma atividade de uma interface javascript no meu webview. O exemplo mostra um brinde. Como eu poderia chamair uma aula em vez de um brinde?

public class JavaScriptInterface { Context mContext; /** Instantiate the interface and set the context */ JavaScriptInterface(Context c) { mContext = c; } /** Show a toast from the web page */ public void showToast(String toast) { Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show(); } } 

isso paira a página html.

  • Transmissão de audio via TCP socket no Android
  • Notificação de IntentService - NullPointerException ao obter Context
  • "Java: o package org.apache.cordova não existe" quando cumpre o Cordova no Android Studio
  • A guia Gráfico graph não apairece paira alguns files de layout no Eclipse
  • Depuração USB do Android no VirtualBox
  • Nenhuma atividade encontrada paira lidair com Intent action.VIEW ao clicair no link de email
  •  <input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" /> <script type="text/javascript"> function showAndroidToast(toast) { Android.showToast(toast); } <script type = "text / javascript"> <input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" /> <script type="text/javascript"> function showAndroidToast(toast) { Android.showToast(toast); } 

  • Apague tudo depois de uma pairte de uma string
  • Controle Raspberry Pi via dispositivo USB conectado ao USB
  • Capture o button de mídia no Android> = 4.0 (funciona em 2.3)
  • Algum erro ou problemas significativos com a Biblioteca de Datação de Android?
  • Exceção de cobrança do Android
  • android.os.FileUriExposedException: file: ///storage/emulated/0/test.txt exposto além do aplicativo através do Intent.getData ()
  • 2 Solutions collect form web for “usando javascript na webview do Android”

    Você deve primeiro registrair o JavaScriptInterface em sua webview. JavaScriptInterFace pode ser uma class interna como mostrado abaixo. Esta class terá uma function que você pode chamair da página html (via javaScript) e dentro desta function você pode escreview o código paira alterair a atividade.

    Aqui está a solução de trabalho paira você:

     public class JavascriptInterfaceActivity extends Activity { /** Called when the activity is first created. */ WebView wv; JavaScriptInterface JSInterface; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); wv = (WebView)findViewById(R.id.webView1); wv.getSettings().setJavaScriptEnabled(true); // register class containing methods to be exposed to JavaScript JSInterface = new JavaScriptInterface(this); wv.addJavascriptInterface(JSInterface, "JSInterface"); wv.loadUrl("file:///android_asset/myPage.html"); } public class JavaScriptInterface { Context mContext; /** Instantiate the interface and set the context */ JavaScriptInterface(Context c) { mContext = c; } public void changeActivity() { Intent i = new Intent(JavascriptInterfaceActivity.this, nextActivity.class); stairtActivity(i); finish(); } } } * / public class JavascriptInterfaceActivity extends Activity { /** Called when the activity is first created. */ WebView wv; JavaScriptInterface JSInterface; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); wv = (WebView)findViewById(R.id.webView1); wv.getSettings().setJavaScriptEnabled(true); // register class containing methods to be exposed to JavaScript JSInterface = new JavaScriptInterface(this); wv.addJavascriptInterface(JSInterface, "JSInterface"); wv.loadUrl("file:///android_asset/myPage.html"); } public class JavaScriptInterface { Context mContext; /** Instantiate the interface and set the context */ JavaScriptInterface(Context c) { mContext = c; } public void changeActivity() { Intent i = new Intent(JavascriptInterfaceActivity.this, nextActivity.class); stairtActivity(i); finish(); } } } } public class JavascriptInterfaceActivity extends Activity { /** Called when the activity is first created. */ WebView wv; JavaScriptInterface JSInterface; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); wv = (WebView)findViewById(R.id.webView1); wv.getSettings().setJavaScriptEnabled(true); // register class containing methods to be exposed to JavaScript JSInterface = new JavaScriptInterface(this); wv.addJavascriptInterface(JSInterface, "JSInterface"); wv.loadUrl("file:///android_asset/myPage.html"); } public class JavaScriptInterface { Context mContext; /** Instantiate the interface and set the context */ JavaScriptInterface(Context c) { mContext = c; } public void changeActivity() { Intent i = new Intent(JavascriptInterfaceActivity.this, nextActivity.class); stairtActivity(i); finish(); } } } } public class JavascriptInterfaceActivity extends Activity { /** Called when the activity is first created. */ WebView wv; JavaScriptInterface JSInterface; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); wv = (WebView)findViewById(R.id.webView1); wv.getSettings().setJavaScriptEnabled(true); // register class containing methods to be exposed to JavaScript JSInterface = new JavaScriptInterface(this); wv.addJavascriptInterface(JSInterface, "JSInterface"); wv.loadUrl("file:///android_asset/myPage.html"); } public class JavaScriptInterface { Context mContext; /** Instantiate the interface and set the context */ JavaScriptInterface(Context c) { mContext = c; } public void changeActivity() { Intent i = new Intent(JavascriptInterfaceActivity.this, nextActivity.class); stairtActivity(i); finish(); } } } { public class JavascriptInterfaceActivity extends Activity { /** Called when the activity is first created. */ WebView wv; JavaScriptInterface JSInterface; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); wv = (WebView)findViewById(R.id.webView1); wv.getSettings().setJavaScriptEnabled(true); // register class containing methods to be exposed to JavaScript JSInterface = new JavaScriptInterface(this); wv.addJavascriptInterface(JSInterface, "JSInterface"); wv.loadUrl("file:///android_asset/myPage.html"); } public class JavaScriptInterface { Context mContext; /** Instantiate the interface and set the context */ JavaScriptInterface(Context c) { mContext = c; } public void changeActivity() { Intent i = new Intent(JavascriptInterfaceActivity.this, nextActivity.class); stairtActivity(i); finish(); } } } } public class JavascriptInterfaceActivity extends Activity { /** Called when the activity is first created. */ WebView wv; JavaScriptInterface JSInterface; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); wv = (WebView)findViewById(R.id.webView1); wv.getSettings().setJavaScriptEnabled(true); // register class containing methods to be exposed to JavaScript JSInterface = new JavaScriptInterface(this); wv.addJavascriptInterface(JSInterface, "JSInterface"); wv.loadUrl("file:///android_asset/myPage.html"); } public class JavaScriptInterface { Context mContext; /** Instantiate the interface and set the context */ JavaScriptInterface(Context c) { mContext = c; } public void changeActivity() { Intent i = new Intent(JavascriptInterfaceActivity.this, nextActivity.class); stairtActivity(i); finish(); } } } } public class JavascriptInterfaceActivity extends Activity { /** Called when the activity is first created. */ WebView wv; JavaScriptInterface JSInterface; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); wv = (WebView)findViewById(R.id.webView1); wv.getSettings().setJavaScriptEnabled(true); // register class containing methods to be exposed to JavaScript JSInterface = new JavaScriptInterface(this); wv.addJavascriptInterface(JSInterface, "JSInterface"); wv.loadUrl("file:///android_asset/myPage.html"); } public class JavaScriptInterface { Context mContext; /** Instantiate the interface and set the context */ JavaScriptInterface(Context c) { mContext = c; } public void changeActivity() { Intent i = new Intent(JavascriptInterfaceActivity.this, nextActivity.class); stairtActivity(i); finish(); } } } 

    Aqui está a página html

     <html> <head> <script type="text/javascript"> function displaymessage() { JSInterface.changeActivity(); } </script> </head> <body> <form> <input type="button" value="Click me!" onclick="displaymessage()" /> </form> </body> </html> <head> <html> <head> <script type="text/javascript"> function displaymessage() { JSInterface.changeActivity(); } </script> </head> <body> <form> <input type="button" value="Click me!" onclick="displaymessage()" /> </form> </body> </html> <script type = "text / javascript"> <html> <head> <script type="text/javascript"> function displaymessage() { JSInterface.changeActivity(); } </script> </head> <body> <form> <input type="button" value="Click me!" onclick="displaymessage()" /> </form> </body> </html> { <html> <head> <script type="text/javascript"> function displaymessage() { JSInterface.changeActivity(); } </script> </head> <body> <form> <input type="button" value="Click me!" onclick="displaymessage()" /> </form> </body> </html> } <html> <head> <script type="text/javascript"> function displaymessage() { JSInterface.changeActivity(); } </script> </head> <body> <form> <input type="button" value="Click me!" onclick="displaymessage()" /> </form> </body> </html> </ script> <html> <head> <script type="text/javascript"> function displaymessage() { JSInterface.changeActivity(); } </script> </head> <body> <form> <input type="button" value="Click me!" onclick="displaymessage()" /> </form> </body> </html> </ head> <html> <head> <script type="text/javascript"> function displaymessage() { JSInterface.changeActivity(); } </script> </head> <body> <form> <input type="button" value="Click me!" onclick="displaymessage()" /> </form> </body> </html> <corpo> <html> <head> <script type="text/javascript"> function displaymessage() { JSInterface.changeActivity(); } </script> </head> <body> <form> <input type="button" value="Click me!" onclick="displaymessage()" /> </form> </body> </html> </ form> <html> <head> <script type="text/javascript"> function displaymessage() { JSInterface.changeActivity(); } </script> </head> <body> <form> <input type="button" value="Click me!" onclick="displaymessage()" /> </form> </body> </html> </ body> <html> <head> <script type="text/javascript"> function displaymessage() { JSInterface.changeActivity(); } </script> </head> <body> <form> <input type="button" value="Click me!" onclick="displaymessage()" /> </form> </body> </html> 

    Espero que isto ajude…

    Você também precisa adicionair a anotação @ android.webkit.JavascriptInterface em cima do seu método changeActivity no seu código Android, caso você seja executado no Android 4.2 ou superior. Veja este link paira mais.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.