Determine whether the status bar is displayed and how to get the height of the status bar, and the tool class list

https://www.2cto.com/kf/201703/605854.html

As mentioned in the above URL, there are two general methods. The second method does not actually need to create a new service.

In fact, it can be used directly like this:

Optimization scheme one:

1. When onCreate:

    private void initStatusBarHelperView() {
        WindowManager wm = (WindowManager) mContext.getSystemService (Context.WINDOW_SERVICE);
        mStatusBarHelperView = new View(mContext);
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams ();
        lp.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
        lp.gravity = Gravity.LEFT | Gravity.TOP;
        lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
        lp.format = PixelFormat.TRANSLUCENT;
        wm.addView(mStatusBarHelperView, lp);
    }

2. When onDestory:

    public void removeStatusBarHelperView() {
        WindowManager wm = (WindowManager) mContext.getSystemService (Context.WINDOW_SERVICE);
        wm.removeView(mStatusBarHelperView);
        mStatusBarHelperView = null;
    }

3. When height is required:

    public int getStatusBarHeight() {
        int[] windowParams = new int[2];
        int[] screenParams = new int[2];
        mStatusBarHelperView.getLocationInWindow(windowParams);
        mStatusBarHelperView.getLocationOnScreen(screenParams);
        return screenParams[1] - windowParams[1];
    }  

 The above method has been tested personally. 

Optimization plan two:

Considering that if it is in the service, it is only removed in the destory, and it is not very good for the newly created view to exist all the time. Therefore, it is more appropriate to call the corresponding methods on onstart and onpusue, but if there are performance problems with frequent calls, it is finally used to delay execution when remove. Finally, a tool class is written, and the following three methods can be called directly:

StatusBarHelper.getInstance().getStatusBarHeight()

StatusBarHelper.getInstance().addStatusBarHelperView();

StatusBarHelper.getInstance().removeStatusBarHelperView();

The tool classes are as follows:

package com.smartisanos.ime.util;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;

import com.smartisanos.ime.IMEApp;

public class StatusBarHelper {
    private Context mContext;
    private View mStatusBarHelperView;
    private static StatusBarHelper mStatusBarHelper = new StatusBarHelper();
    private static final int MSG_INIT_STATUS_BAR = 1;
    private static final int MSG_REMOVE_STATUS_BAR = 2;
    private static final long MSG_REMOVE_DELAY_TIME = 500L;

    private Handler mStatusBarHandler = new Handler () {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_INIT_STATUS_BAR:
                    mStatusBarHandler.removeMessages(MSG_REMOVE_STATUS_BAR);
                    initStatusBarHelperView();
                    break;
                case MSG_REMOVE_STATUS_BAR:
                    WindowManager wm = (WindowManager) mContext.getSystemService (Context.WINDOW_SERVICE);
                    wm.removeView(mStatusBarHelperView);
                    mStatusBarHelperView = null;
                    break;
            }
        }
    };

    private StatusBarHelper() {
        mContext = IMEApp.getContext();
    }

    public static StatusBarHelper getInstance() {
        return mStatusBarHelper;
    }

/* //Because there is no time-consuming operation in the construction method of this class, there is no need to perform the new operation in getInstance(), and you can directly new when defining the variable. Therefore, the following method is not used
    public static StatusBarHelper getInstance() {
        if (mStatusBarHelper == null) {
            mStatusBarHelper = new StatusBarHelper();
        }
        return mStatusBarHelper;
    }
    */
    private void initStatusBarHelperView() {
        if (mStatusBarHelperView != null) {
            return;
        }
        WindowManager wm = (WindowManager) mContext.getSystemService (Context.WINDOW_SERVICE);
        mStatusBarHelperView = new View(mContext);
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams ();
        lp.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
        lp.gravity = Gravity.LEFT | Gravity.TOP;
        lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
        lp.format = PixelFormat.TRANSLUCENT;
        wm.addView(mStatusBarHelperView, lp);
    }

    public void addStatusBarHelperView() {
        Message message = mStatusBarHandler.obtainMessage(StatusBarHelper.MSG_INIT_STATUS_BAR);
        mStatusBarHandler.sendMessage(message);
    }

    public void removeStatusBarHelperView() {
        Message message = mStatusBarHandler.obtainMessage(StatusBarHelper.MSG_REMOVE_STATUS_BAR);
        mStatusBarHandler.sendMessageDelayed(message, StatusBarHelper.MSG_REMOVE_DELAY_TIME);
    }

    public int getStatusBarHeight() {
        if (mStatusBarHelperView == null) {
            return 0;
        }
        int[] windowParams = new int[2];
        int[] screenParams = new int[2];
        mStatusBarHelperView.getLocationInWindow(windowParams);
        mStatusBarHelperView.getLocationOnScreen(screenParams);
        return screenParams[1] - windowParams[1];
    }
}

  

 

 

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325162982&siteId=291194637