zoukankan      html  css  js  c++  java
  • 如何在cordova-plugin-statusbar插件基础上增加获取状态栏高度功能

      最近做项目,有一个功能需求需要获取状态栏高度,但是遍寻各种插件,都没有此功能,自己又不会写原生代码,很无奈。但是在cordova-plugin-statusbar插件git仓库的issues里,看到有人提出了同样的需求,并且有人实现了这个功能且发起了pr,只是插件官方并没有合并到主分支上且release新版本。于是,自己去合并请求的仓库里找到了相关代码,加到了自己本地的插件代码里。目前仅实践了android,所以先把安卓相关的改动贴出来记录一下(红色代码部分),之后实践了iOS后再来补上。

      PS:如果已经添加了platform的话,直接修改plugins里面的内容,打包的时候是不会生效的,所以要么去平台编译后的代码里添加修改,或者修改后重新添加platform

    • pluginscordova-plugin-statusbarwwwstatusbar.js
    var StatusBar = {
        
        height: function (onSuccess, onError) {
            exec(onSuccess, onError, "StatusBar", "height", []);
        },
    
        isVisible: true,
    
        ……
    
    };
    • pluginscordova-plugin-statusbarsrcandroidStatusBar.java
    public boolean execute(final String action, final CordovaArgs args, final CallbackContext callbackContext) throws JSONException {
            LOG.v(TAG, "Executing action: " + action);
            final Activity activity = this.cordova.getActivity();
            final Window window = activity.getWindow();
    
            if ("_ready".equals(action)) {
                boolean statusBarVisible = (window.getAttributes().flags & WindowManager.LayoutParams.FLAG_FULLSCREEN) == 0;
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, statusBarVisible));
                return true;
            }
            
            if ("height".equals(action)) {
                float statusBarHeight = getStatusBarHeight();
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, statusBarHeight));
                return true;
            }
            ……        
    }
    ……
    
      private float getStatusBarHeight() {
            float statusBarHeight = 0;
            int resourceId = cordova.getActivity().getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                float scaleRatio = cordova.getActivity().getResources().getDisplayMetrics().density;
                statusBarHeight = cordova.getActivity().getResources().getDimension(resourceId) / scaleRatio;
            }
            return statusBarHeight;
        }
    
        private void setStatusBarBackgroundColor(final String colorPref) {
            if (Build.VERSION.SDK_INT >= 21) {
                if (colorPref != null && !colorPref.isEmpty()) {
                    final Window window = cordova.getActivity().getWindow();
                    // Method and constants not available on all SDKs but we want to be able to compile this code with any SDK
                    window.clearFlags(0x04000000); // SDK 19: WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                    window.addFlags(0x80000000); // SDK 21: WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                    try {
                        // Using reflection makes sure any 5.0+ device will work without having to compile with SDK level 21
                        window.getClass().getMethod("setStatusBarColor", int.class).invoke(window, Color.parseColor(colorPref));
                    } catch (IllegalArgumentException ignore) {
                        LOG.e(TAG, "Invalid hexString argument, use f.i. '#999999'");
                    } catch (Exception ignore) {
                        // this should not happen, only in case Android removes this method in a version > 21
                        LOG.w(TAG, "Method window.setStatusBarColor not found for SDK level " + Build.VERSION.SDK_INT);
                    }
                }
            }
        }
    
    ……
    • pluginscordova-plugin-statusbarsrcrowserStatusBarProxy.js
    ……

    module.exports = { isVisible: false, height: 0, styleBlackTranslucent:notSupported, styleDefault:notSupported, styleLightContent:notSupported, styleBlackOpaque:notSupported, overlaysWebView:notSupported, styleLightContect: notSupported, backgroundColorByName: notSupported, backgroundColorByHexString: notSupported, hide: notSupported, show: notSupported, _ready:notSupported };

    ……

  • 相关阅读:
    将动态aspx页面转换成为静态html页面的几种方法
    IHttpHandler 概述
    .Net,你究竟有多慢
    Js离开提示
    vs2008Webconfig文件
    什么是URL转发和一个IP建多个Web站点主机头名法
    sqlserver 统计sql语句大全收藏
    web.config 读写操作
    httpModules 与 httpHandlers
    简单的介绍一下常用的正规表示式:
  • 原文地址:https://www.cnblogs.com/happymental/p/12876891.html
Copyright © 2011-2022 走看看