RN- 判斷安卓是否是全面屏及是否開啓了全面屏

RNScreenpxModule 安卓的橋接模塊,
用來判斷機型是否是全面屏
以及是否開啓了全面屏,
通過這個模塊, 來適配一些RN中安卓全畫幅的UI高度

package com.regan.ebankhome;

import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.os.Build;
import android.provider.Settings;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import androidx.core.content.ContextCompat;

import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.uimanager.IllegalViewOperationException;

import java.util.HashMap;
import java.util.Map;

public class RNScreenpxModule extends ReactContextBaseJavaModule {


    private int w = 0; // 當前設備的寬度方向 像素數
    private int h = 0; // 當前設備的寬度方向 像素數
    private volatile static boolean mHasCheckAllScreen;
    private volatile static boolean mIsAllScreenDevice;   // 是否全面屏
    private volatile static boolean notNavigationBarExist; // 不存在虛擬導航器 (即真正的使用全面屏)


    public RNScreenpxModule(ReactApplicationContext reactApplicationContext) {
        super(reactApplicationContext);

        WindowManager wd = (WindowManager) reactApplicationContext.getSystemService(Context.WINDOW_SERVICE);

        // 1. 獲取當前安卓設備的真實像素
        w = RNScreenpxModule.getRealPXFromScreenW(reactApplicationContext);
        h = RNScreenpxModule.getRealPXFromScreenH(reactApplicationContext);

        // 2. 判斷是否爲全面屏
        mIsAllScreenDevice = RNScreenpxModule.isAllScreenDevice(reactApplicationContext);
        Log.e("ssss", mIsAllScreenDevice + "===0=== 全面屏嗎??????");

        if (mIsAllScreenDevice) {
            Log.e("ssss", notNavigationBarExist + "===0=== 開啓了嗎??????");
            notNavigationBarExist = Settings.Global.getInt(reactApplicationContext.getContentResolver(), "force_fsg_nav_bar", 0) != 0;
            Log.e("ssss", notNavigationBarExist + "===0=== 開啓了嗎??????");

        }


    }

    /**
     * 獲取當前安卓設備的真實像素
     *
     * @param context
     */
    public static int getRealPXFromScreenH(Context context) {
        WindowManager wd = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display defaultDisplay = wd.getDefaultDisplay();
        Point point = new Point();
        defaultDisplay.getSize(point);
        int x = point.x;
        int y = point.y;
        return y;
    }


    /**
     * 獲取當前安卓設備的真實像素
     *
     * @param context
     */
    public static int getRealPXFromScreenW(Context context) {
        WindowManager wd = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display defaultDisplay = wd.getDefaultDisplay();
        Point point = new Point();
        defaultDisplay.getSize(point);
        int x = point.x;
        int y = point.y;
        return x;
    }



    /**
     * 判斷 安卓設置是否 全面屏
     *
     * @param context
     * @return
     */
    public static boolean isAllScreenDevice(Context context) {
        if (mHasCheckAllScreen) {
            return mIsAllScreenDevice;
        }
        mHasCheckAllScreen = true;
        mIsAllScreenDevice = false;
        // 低於 API 21的,都不會是全面屏。。。
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            return false;
        }
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        if (windowManager != null) {
            Display display = windowManager.getDefaultDisplay();
            Point point = new Point();
            display.getRealSize(point);
            float width, height;
            if (point.x < point.y) {
                width = point.x;
                height = point.y;
            } else {
                width = point.y;
                height = point.x;
            }
            if (height / width >= 1.97f) {
                mIsAllScreenDevice = true;
            }
        }
        return mIsAllScreenDevice;
    }


    /**
     * 這是定義JS端要調用的模塊名,比如"ScreenpxModule"
     *
     * @return
     */
    @Override
    public String getName() {
        return "ScreenpxModule";
    }


    /**
     * 獲取屏幕寬度方向像素
     * 這是用作導出一個方法給JavaScript使用,Java方法需要使用註解@ReactMethod
     * 所以到時候JS調用就是React.NativeModules.ScreenpxModule.getScreenW().then()...
     *
     * @param promise
     */
    @ReactMethod
    public void getScreenW(Promise promise) {
        promise.resolve(w);
    }

    @ReactMethod
    public void getScreenH(Promise promise) {
        promise.resolve(h);
    }

    @ReactMethod
    public void checkFullScreen(Promise promise) {

        Boolean fullScreenAndNotNavigationBarExist = mIsAllScreenDevice && notNavigationBarExist;

        // 全面屏切啓用了全面屏
        if (fullScreenAndNotNavigationBarExist) {
            promise.resolve(true);
        } else {
            // 非全面屏或 全面屏但是未啓用全面屏功能
            promise.resolve(false);
        }
    }

}

使用

  componentDidMount() {
    !isiOS() ? LocalStoreUtils.isUseFullScreen_Android('get')
      .then((res) => {
        this.setState({chooseViewheight: res ? app.screenH : app.height});
      }) : null;

  }
export const height = Dimensions.get("window").height;
export const screenH = Dimensions.get("screen").height;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章