android webView js 使用
1、js調用java
1、1 js代碼
<script type="text/javascript">
function call(){
window.androidInterface.call('02585818031');
}
</script>
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); //設置無標題
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 調用js
2.1、java代碼,webView為WebView對象
// 無參數(shù)調用
webView.loadUrl("javascript:javacalljs()");
// 傳遞參數(shù)調用
webView.loadUrl("javascript:javacalljswithargs(" + "'hello world'" + ")");
2.2、js代碼
function javacalljs(){
document.getElementById("content").innerHTML +=
"<br\>java調用了js函數(shù)";
}
function javacalljswithargs(arg){
document.getElementById("content").innerHTML +=
("<br\>"+arg);
}

創(chuàng)新互聯(lián)專注于企業(yè)網絡營銷推廣、網站重做改版、美蘭網站定制設計、自適應品牌網站建設、H5建站、商城網站定制開發(fā)、集團公司官網建設、成都外貿網站制作、高端網站制作、響應式網頁設計等建站業(yè)務,價格優(yōu)惠性價比高,為美蘭等各大城市提供網站開發(fā)制作服務。
分享題目:androidWebViewjava與js相互調用
當前地址:http://chinadenli.net/article16/goicdg.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供Google、域名注冊、品牌網站建設、全網營銷推廣、App設計、網頁設計公司
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)