android webView js 使用
1、js調(diào)用java
1、1 js代碼
1.2、java代碼
package com.example.webview;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
@SuppressLint("JavascriptInterface")
public class MainActivity extends ActionBarActivity {
private WebView webView;
private int screenHeight;
private int screenWidth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); //設(shè)置無標題
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.web_view);
WebSettings webSettings = webView.getSettings();
webSettings.setBuiltInZoomControls(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(new MyWebViewClient());
webView.addJavascriptInterface(new MyAndroidInterface(), "androidInterface");
String url ="file:///android_asset/tangbangjidian/fuwu.html";
//String url ="file:///android_asset/baitian/index.html";
webView.loadUrl(url);
}
class MyAndroidInterface{
public MyAndroidInterface(){};
@JavascriptInterface //注意:加上這行注解
public void call(String number){
//用intent啟動撥打電話
Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+number));
startActivity(intent);
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack(); //goBack()表示返回WebView的上一頁面
return false;
}
return true;
}
private class MyWebViewClient extends WebViewClient{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);// 當打開新鏈接時,使用當前的 WebView,不會使用系統(tǒng)其他瀏覽器
return true;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
2、java 調(diào)用js
2.1、java代碼,webView為WebView對象
// 無參數(shù)調(diào)用
webView.loadUrl("javascript:javacalljs()");
// 傳遞參數(shù)調(diào)用
webView.loadUrl("javascript:javacalljswithargs(" + "'hello world'" + ")");
2.2、js代碼
function javacalljs(){
document.getElementById("content").innerHTML +=
"
java調(diào)用了js函數(shù)";
}
function javacalljswithargs(arg){
document.getElementById("content").innerHTML +=
("
"+arg);
}
創(chuàng)新互聯(lián)專注于企業(yè)網(wǎng)絡(luò)營銷推廣、網(wǎng)站重做改版、美蘭網(wǎng)站定制設(shè)計、自適應品牌網(wǎng)站建設(shè)、H5建站、商城網(wǎng)站定制開發(fā)、集團公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為美蘭等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。