RN- 判断安卓是否是全面屏及是否开启了全面屏
RNScreenpxModule 安卓的桥接模块,用来判断机型是否是全面屏以及是否开启了全面屏,通过这个模块, 来适配一些RN中安卓全画幅的UI高度package com.regan.ebankhome;import android.app.Activity;import android.content.Context;import android.graphics.Point;import an
·
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;
更多推荐
已为社区贡献10条内容
所有评论(0)