## 安卓调JS方法
webview是安卓与JS交互的桥梁,我们是通过webview来实现他们的相互调用。
1.先在Layout文件写webview控件。
```
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:clipChildren="false"> android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent"> android:id="@+id/web" android:layout_width="match_parent" android:layout_height="match_parent">
```
2.在MainActivity里设置webview
```
package com.xq.jztc.activity;
import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ClipData;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.alipay.sdk.app.H5PayCallback;
import com.alipay.sdk.app.PayTask;
import com.alipay.sdk.util.H5PayResultModel;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.umeng.socialize.UMShareAPI;
import com.xq.jztc.JavaScriptinterface;
import com.xq.jztc.R;
import com.yzq.zxinglibrary.common.Constant;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
import io.reactivex.functions.Consumer;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.web);
setWebView();
}
}
```
3.设置webView
```
private void setWebView() {
assert webView != null;
webView.setVerticalScrollbarOverlay(true);
//加载网页
webView.loadUrl(https://www.baidu.com/);
//android js交互
javaScriptinterface = new JavaScriptinterface(this, webView);
webView.addJavascriptInterface(javaScriptinterface, "android");
//处理各种通知 & 请求事件
webView.setWebViewClient(webViewClient);
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true); //可执行js
settings.setDefaultZoom(WebSettings.ZoomDensity.MEDIUM); //设置页面默认缩放密度
settings.setDefaultTextEncodingName("UTF-8"); //设置默认的文本编码名称,以便在解码html页面时使用
settings.setAllowContentAccess(true); //启动或禁用WebView内的内容URL访问
settings.setAppCacheEnabled(false); //设置是否应该启用应用程序缓存api
settings.setBuiltInZoomControls(false); //设置WebView是否应该使用其内置的缩放机制
settings.setUseWideViewPort(true); //设置WebView是否应该支持viewport
settings.setLoadWithOverviewMode(true); //不管WebView是否在概述模式中载入页面,将内容放大适合屏幕宽度
settings.setCacheMode(WebSettings.LOAD_NO_CACHE); //重写缓存的使用方式
settings.setJavaScriptCanOpenWindowsAutomatically(true); //告知js自动打开窗口
settings.setLoadsImagesAutomatically(true); //设置WebView是否应该载入图像资源
settings.setAllowFileAccess(true); //启用或禁用WebView内的文件访问
settings.setDomStorageEnabled(true); //设置是否启用了DOM存储API,默认为false
settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
settings.setSavePassword(false);
webView.setWebChromeClient(new WebChromeClient() {
// For Android < 3.0
public void openFileChooser(ValueCallback
uploadMessage = valueCallback;
openImageChooserActivity();
}
// For Android >= 3.0
public void openFileChooser(ValueCallback valueCallback, String acceptType) {
uploadMessage = valueCallback;
openImageChooserActivity();
}
//For Android >= 4.1
public void openFileChooser(ValueCallback
uploadMessage = valueCallback;
openImageChooserActivity();
}
// For Android >= 5.0
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback
uploadMessageAboveL = filePathCallback;
openImageChooserActivity();
return true;
}
});
}
```
## JS调安卓方法
@JavascriptInterface
public void JS的方法名(参数) {
安卓要走的方法体
}
*例子:*
```
//打电话
@JavascriptInterface
public void apptel(String tel) {
Intent intent = new Intent(Intent.ACTION_DIAL);
Uri data = Uri.parse("tel:" + tel);
intent.setData(data);
context.startActivity(intent);
}
```