美文网首页
Android与js的交互

Android与js的交互

作者: 我是你森哥哥 | 来源:发表于2017-06-09 13:44 被阅读0次

在本人目前的开发项目中 ,有些内容是使用H5界面展示的,当中避免不了要用到Android与js的交互,以下为个人在项目中的使用情况

1. webView的加载

若想实现交互,这一行代码必须加上
settings.setJavaScriptEnabled(true); //是否调用服务端js方法

 WebSettings settings = webView.getSettings();               //获取设置管理
            settings.setJavaScriptEnabled(true);                        //是否调用服务端js方法
            //两者一起使用
            settings.setSupportZoom(false);                               //是否支持放大缩小
            settings.setBuiltInZoomControls(false);                          //是否显示放大缩小控件
            //设置webview推荐使用的窗口,设置为true。
            settings.setUseWideViewPort(true);
            //第二个方法是设置webview加载的页面的模式,也设置为true。
            settings.setLoadWithOverviewMode(true);
            //解决webview缓存问题
            if (Build.VERSION.SDK_INT >= 19) {
                settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
            }
            webView.addJavascriptInterface(new Textinterface(), "android");

            webView.setWebViewClient(new WebViewClient() {
                /**
                 * 拦截 url 跳转,在里边添加点击链接跳转或者操作
                 */
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    //返回值是true的时候控制去WebView打开,为false调用系统浏览器或第三方浏览器
                    if (url.equals("https://www.baidu.com/")) {
                        //                    supportDetails();
                    } else {
                        view.loadUrl(url);
                    }
                    return true;
                }

                /**
                 * 在开始加载网页时会回调
                 */
                public void onPageStarted(WebView view, String url, Bitmap favicon) {
                }

                /**
                 * 在结束加载网页时会回调
                 */
                public void onPageFinished(WebView view, String url) {
                }

                /**
                 * 加载错误的时候会回调,在其中可做错误处理,比如再请求加载一次,或者提示404的错误页面
                 */
                public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                    statelayoutCollectdetails.showErrorView();
                }

                /**
                 * 当接收到https错误时,会回调此函数,在其中可以做错误处理
                 */
                public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                    statelayoutCollectdetails.showErrorView();
                }

                /**
                 * 在每一次请求资源时,都会通过这个函数来回调
                 */
                public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
                    return null;
                }
            });

            webView.loadUrl(h5_url);


2. 将对象注入到js中



    /**
     * android 与js的交互
     */
    public class Textinterface {
        
        @JavascriptInterface
        public void show() {
            supportDetails();
        }

        @JavascriptInterface
        public void show(String s) {
            supportDetails();
        }  
    }

3. 前端同学需要在代码中写上

<button onClick="window.android.show()">启动hello world Activity</button>

4. 如何从浏览器打开本地的app

  • 【1】创建activity 在清单文件中配置
  <!-- H5界面跳转本地APP配置 -->
        <activity
            android:name="com.example.xingchouwang.view.activity.SchemeCenterActivity"
            android:theme="@android:style/Theme.NoDisplay">
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>

                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>

//这个参数非常重要
                <data android:scheme="Myapp"/>
            </intent-filter>
        </activity>

  • 【2】在activity中接受参数
Intent i_getvalue = getIntent();
        String action = i_getvalue.getAction();

        if (Intent.ACTION_VIEW.equals(action)) {

            Uri data = i_getvalue.getData();
            String host = data.getHost();
            Log.i(this,data+"");
            Log.i(this,host+"");//548,后边会提到这个参数是那里传入的
            }
        }

  • 【3】前端同学需要做的
<a href="intent://548#Intent;scheme=Myapp;package=com.examplle.app;end">打开APP</a>

大神的博客:http://blog.csdn.net/u013424496/article/details/51991541
大神的博客:http://blog.csdn.net/qq_23089525/article/details/52769315
大神的博客:http://www.2cto.com/kf/201604/499119.html
大神的博客:http://www.cnblogs.com/shadajin/p/5724117.html

相关文章

网友评论

      本文标题:Android与js的交互

      本文链接:https://www.haomeiwen.com/subject/netfqxtx.html