Java and js code intermodulation sample code

  • 2020-04-01 02:09:21
  • OfStack

When using HTML5 to do cross-platform application development, try to use Java and js methods intermodulation problems, may be a bit difficult for beginners, here to share some of their own in the actual development process, I hope to help you:

First is the js code call Java code introduction:
 
public class CzingLBWebMain extends DroidGap { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
this.loadUrl(file:///android_asset/www/czingIndexHome.html); 
WebSettings webSettings = this.appView.getSettings(); 
webSettings.setLoadWithOverviewMode(true); 
webSettings.setSavePassword(false); 
webSettings.setSaveFormData(false); 
webSettings.setSupportZoom(false); 
webSettings.setAllowFileAccess(true); 
webSettings.setJavaScriptEnabled(true); 
webSettings.setPluginState(WebSettings.PluginState.ON); 
//Add a Java instance to js so that the js code can invoke the Java code
this.appView.addJavascriptInterface(this, "appDownloader"); 
} 
public void callBackJava(){ 
} 
} 

Then can czingIndexHome. HTML reference js file directly through the window. The appDownloader. CallBackJava () to directly call the above callBackJava () method.

Then there is how to call js code through Java code:
In Java, it is implemented in the following way:
 
String resUrl="aa"; 
String resId="bb"; 
String callBack="javascript:callBackServive('"+resUrl+"','"+resId+"')";//Notice the method that passes more than two parameters
this.appView.loadUrl(callBack); 

CallBackServive is the method implemented in js.

Related articles: