js与android原生交互
package com.liuhao.mysecond;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.ValueCallback;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import com.liuhao.mysecond.WebView.WebViewActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onResume() {
/**
* 设置为横屏
*/
// if(getRequestedOrientation()!= ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
// }
super.onResume();
}
@SuppressLint("JavascriptInterface")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WebView myWebView = findViewById(R.id.WV_Id);
myWebView.clearCache(true);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
myWebView.getSettings().setDomStorageEnabled(true);
myWebView.getSettings().setAllowFileAccess(true);
myWebView.addJavascriptInterface(new JavaScriptinterface(this), "android");
myWebView.loadUrl("http://10.8.106.67:3000/test.html");
myWebView.setWebViewClient(new MyWebViewClient());
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void sendMessage(View view) {
WebView myWebView = findViewById(R.id.WV_Id);
myWebView.evaluateJavascript("javascript:AndroidcallJS()", new ValueCallback<String>() {
@Override
public void onReceiveValue(String value) {
//此处为 js 返回的结果
Log.i("刘浩:", value);
}
});
}
}
class JavaScriptinterface {
Context context;
public JavaScriptinterface(Context c) {
context = c;
}
/**
* 与js交互时用到的方法,在js里直接调用的
*/
@JavascriptInterface
public void click(String ssss) {
Toast.makeText(context, ssss, Toast.LENGTH_LONG).show();
}
}
class MyWebViewClient extends WebViewClient {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override //WebView代表是当前的WebView
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
//表示在当前的WebView继续打开网页
view.loadUrl(request.getUrl().toString());
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
}
<div style="margin-top:200px"></div>
<span id="liu" onclick="AndroidcallJS()" href="myapp://">打开微信</span>
<div id="show"></div>
<button id="btn" onclick="window.android.click('77777777777')">掉原生</button>
<script>
function AndroidcallJS(){
document.getElementById("show").innerHTML="Android调用了JS的AndroidcallJS方法"
return "99999999999999"
}
//document.getElementById("liu").click()
</script>
请爱好前端技术的朋友,联系我,有问题大家一起讨论