捕获异常,js交互

//捕获异常
https://blog.csdn.net/qq_43559002/article/details/88052448
//js交互
1放在创建assets的文件夹

在这里插入图片描述
2 MainActivity中

 public class MainActivity extends AppCompatActivity {

    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView = (WebView) findViewById(R.id.webview);
       //adnroid调用js方法
        WebSettings webSettings = webView.getSettings();
        //设置支持js
        webSettings .setJavaScriptEnabled(true);

       // webView.loadUrl("file:///android_asset/info.html");
        webView.loadUrl("file:///android_asset/info.html");

      //  webView.loadUrl("https://www.baidu.com/baidu?tn=monline_3_dg&ie=utf-8&wd=selenium+jar+2.4");


        //交互  adnroid调用js方法  //长按事件
        webView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {

                //adnroid调用js方法
               webView.loadUrl("javascript:test1()");//可以传参数
               // webView.evaluateJavascript();//Android 4.4

             //   webView.loadUrl("javascript:(document.getElementsByTagName('img')[0].src='http://p4.so.qhmsg.com/bdr/326__/t0168082f62f4b041de.jpg'; )");
                return false;
            }
        });


        //1,js调用安卓   协议
        webView.addJavascriptInterface(this, "android");





    }
    //js调用安卓方法需要添加注解
    @JavascriptInterface
    public void jump() {//传参

        Toast.makeText(MainActivity.this, "js调用adnroid方法", Toast.LENGTH_SHORT).show();
        Intent intent =new Intent(this,Main2Activity.class);
        startActivity(intent);
    }
  //页面销毁的时候销毁webView
    @Override
    protected void onDestroy() {
        super.onDestroy();
        webView.destroy();
    }
}

3 Main2Activity中

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        final WebView webview = (WebView) findViewById(R.id.webview);
        //设置webview支持js交互
        webview.getSettings().setJavaScriptEnabled(true);
        //加载页面
        webview.loadUrl("file:///android_asset/infos.html");

        //设置弹出Alert
        webview.setWebChromeClient(new WebChromeClient(){
            //title就是网页标题
            @Override
            public void onReceivedTitle(WebView view, String title) {//此方法可获得网页标题
                Log.i("xxx",view.getTitle());
                super.onReceivedTitle(view, title);

            }

            @Override
            public boolean onJsAlert(WebView view, String url, String message,
                                     JsResult result) {
                // TODO Auto-generated method stub
                //false弹出  true和super不弹
                return false;
            }

            @Override
            public boolean onJsPrompt(WebView view, String url, String message,
                                      String defaultValue, JsPromptResult result) {
                // TODO Auto-generated method stub
                return super.onJsPrompt(view, url, message, defaultValue, result);
            }
            /**处理定位的相关,否则WebView不会开启定位功能,类似百度地图这样的就没法定位*/
            @Override
            public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
                callback.invoke(origin, true, false);              //这个必须有
                super.onGeolocationPermissionsShowPrompt(origin, callback);
            }
        });

        webview.setWebViewClient(new WebViewClient(){
            //拦截浏览器加载页面,让WebView加载页面
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                view.loadUrl("url");
                return true;
            }
        });

  //js调用andorid方法
        webview.addJavascriptInterface(this, "android");
    }

    //js调用andorid方法 并传值
    @JavascriptInterface
    public void showUserInfo(String name,String pwd){
        Toast.makeText(this, name+","+pwd, Toast.LENGTH_LONG).show();


        /*Intent intent =new Intent(this,Main3Activity.class);
        startActivity(intent);*/
    }
}


在这里插入图片描述

  //button按钮
        js = findViewById(R.id.js);
        webview = findViewById(R.id.webview);
        //设置支持js
        webview.getSettings().setJavaScriptEnabled(true);
        //加载
        webview.loadUrl("file:///android_asset/javascript.html");
        //注册
        js.setOnClickListener(this);
        
   @SuppressLint("JavascriptInterface")
    @Override
    public void onClick(View v) {
        switch (v.getId()){
              case R.id.js:
              //android调用js方法
                webview.loadUrl("javascript:callJS()");

              //  webview.setWebChromeClient(new WebChromeClient());

   		        webview.setWebChromeClient(new WebChromeClient(){
                    @Override
                    public void onReceivedTitle(WebView view, String title) {
                        super.onReceivedTitle(view, title);
                    }


                    @Override
                    public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                        return false;
                    }

                    @Override
                    public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
                        return super.onJsPrompt(view, url, message, defaultValue, result);
                    }



                    @Override
                    public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
                        callback.invoke(origin,true,false);//这个必须有
                        super.onGeolocationPermissionsShowPrompt(origin, callback);
                    }
                });

                webview.setWebViewClient(new WebViewClient(){
                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                        view.loadUrl("url");
                        return true;
                    }
                });


                break;



        }



    }


©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值