MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
print();
// Tools.hideBottomUIMenu(this);
}
private void print() {
print("是否存在导航栏 :" + Tools.checkDeviceHasNavigationBar(this));
print("不包含虚拟导航栏的高度 : " + Tools.getNoHasVirtualKey(this));
print("包含虚拟导航栏的高度 : " + Tools.getHasVirtualKey(this));
print("getRealHight =" + (Tools.getRealHight(this)));
print("getScreenHeight =" + (Tools.getScreenHeight(this)));
print("导航栏高度:" + Tools.getNavigationBarHeight(this));
print("DecorView高度:" + getWindow().getDecorView().getHeight());
print("华为手机 :" + Tools.isHUAWEI());
}
private void print(String msg) {
Log.i("Nj", msg);
}
}
Tools.java
package com.example.momo.hasnavigationbar;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.res.Resources;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.WindowManager;
import java.lang.reflect.Method;
/**
* Created by momo on 2017/11/22.
*/
public class Tools {
/**
* 获取屏幕尺寸,但是不包括虚拟功能高度
*
* @return
*/
public static int getNoHasVirtualKey(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
int height = windowManager.getDefaultDisplay().getHeight();
return height;
}
/**
* 通过反射,获取包含虚拟导航栏的整体屏幕高度
*
* @return
*/
public static int getHasVirtualKey(Context context) {
int dpi = 0;
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
DisplayMetrics dm = new DisplayMetrics();
@SuppressWarnings("rawtypes")
Class c;
try {
c = Class.forName("android.view.Display");
@SuppressWarnings("unchecked")
Method method = c.getMethod("getRealMetrics", DisplayMetrics.class);
method.invoke(display, dm);
dpi = dm.heightPixels;
} catch (Exception e) {
e.printStackTrace();
}
return dpi;
}
/**
* 获取屏幕实际高度(也包含虚拟导航栏)
*
* @param context
* @return
*/
public static int getRealHight(Context context) {
DisplayMetrics displayMetrics = null;
if (displayMetrics == null) {
displayMetrics = new DisplayMetrics();
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getRealMetrics(displayMetrics);
}
return displayMetrics.heightPixels;
}
/**
* 判断是否存在NavigationBar
*/
public static boolean checkDeviceHasNavigationBar(Context context) {
boolean hasNavigationBar = false;
Resources rs = context.getResources();
int id = rs.getIdentifier("config_showNavigationBar", "bool", "android");
if (id > 0) {
hasNavigationBar = rs.getBoolean(id);
}
try {
Class systemPropertiesClass = Class.forName("android.os.SystemProperties");
Method m = systemPropertiesClass.getMethod("get", String.class);
String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys");
if ("1".equals(navBarOverride)) {
hasNavigationBar = false;
} else if ("0".equals(navBarOverride)) {
hasNavigationBar = true;
}
} catch (Exception e) {
}
return hasNavigationBar;
}
/**
* 是否是华为
*/
public static boolean isHUAWEI() {
print(android.os.Build.MANUFACTURER);
return android.os.Build.MANUFACTURER.equals("Huawei");
}
/**
* 获取导航栏高度
*
* @param context
* @return
*/
public static int getNavigationBarHeight(Context context) {
boolean hasMenuKey = ViewConfiguration.get(context).hasPermanentMenuKey();
boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
if (!hasMenuKey && !hasBackKey) {
//没有物理按钮(虚拟导航栏)
print("没有物理按钮(虚拟导航栏)");
Resources resources = context.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
//获取NavigationBar的高度
int height = resources.getDimensionPixelSize(resourceId);
return height;
} else {
print("有物理导航栏,小米非全面屏");
//有物理导航栏,小米非全面屏
return 0;
}
}
/**
* 隐藏虚拟按键,并且设置成全屏
*/
public static void hideBottomUIMenu(Context context) {
Activity activity = null;
if (context instanceof Activity) {
activity = (Activity) context;
}
if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
View v = activity.getWindow().getDecorView();
v.setSystemUiVisibility(View.GONE);
} else if (Build.VERSION.SDK_INT >= 19) {
//for new api versions.
View decorView = activity.getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
| View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
| View.SYSTEM_UI_FLAG_IMMERSIVE;
decorView.setSystemUiVisibility(uiOptions);
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
}
public static DisplayMetrics getDisplayMetrics(Context context) {
return context.getResources().getDisplayMetrics();
}
/**
* 获取屏幕分辨率高度 *
*/
public static int getScreenHeight(Context context) {
return getDisplayMetrics(context).heightPixels;
}
private static void print(String msg) {
Log.i("Nj", msg);
}
}
测试log
虚拟键不常驻:
12-11 14:46:12.923 26357-26357/com.example.momo.hasnavigationbar I/Nj: 是否存在导航栏 :true
12-11 14:46:12.924 26357-26357/com.example.momo.hasnavigationbar I/Nj: 不包含虚拟导航栏的高度 : 2030
12-11 14:46:12.925 26357-26357/com.example.momo.hasnavigationbar I/Nj: 包含虚拟导航栏的高度 : 2160
12-11 14:46:12.926 26357-26357/com.example.momo.hasnavigationbar I/Nj: getRealHight =2160
12-11 14:46:12.926 26357-26357/com.example.momo.hasnavigationbar I/Nj: getScreenHeight =2030
12-11 14:46:12.927 26357-26357/com.example.momo.hasnavigationbar I/Nj: 没有物理按钮(虚拟导航栏)
12-11 14:46:12.927 26357-26357/com.example.momo.hasnavigationbar I/Nj: 导航栏高度:130
12-11 14:46:12.928 26357-26357/com.example.momo.hasnavigationbar I/Nj: DecorView高度:0
12-11 14:46:12.928 26357-26357/com.example.momo.hasnavigationbar I/Nj: Xiaomi
12-11 14:46:12.928 26357-26357/com.example.momo.hasnavigationbar I/Nj: 华为手机 :false
虚拟键常驻:
12-11 14:49:44.769 31393-31393/com.example.momo.hasnavigationbar I/Nj: 是否存在导航栏 :true
12-11 14:49:44.770 31393-31393/com.example.momo.hasnavigationbar I/Nj: 不包含虚拟导航栏的高度 : 2030
12-11 14:49:44.770 31393-31393/com.example.momo.hasnavigationbar I/Nj: 包含虚拟导航栏的高度 : 2160
12-11 14:49:44.771 31393-31393/com.example.momo.hasnavigationbar I/Nj: getRealHight =2160
12-11 14:49:44.771 31393-31393/com.example.momo.hasnavigationbar I/Nj: getScreenHeight =2030
12-11 14:49:44.771 31393-31393/com.example.momo.hasnavigationbar I/Nj: 没有物理按钮(虚拟导航栏)
12-11 14:49:44.772 31393-31393/com.example.momo.hasnavigationbar I/Nj: 导航栏高度:130
12-11 14:49:44.772 31393-31393/com.example.momo.hasnavigationbar I/Nj: DecorView高度:0
12-11 14:49:44.772 31393-31393/com.example.momo.hasnavigationbar I/Nj: Xiaomi
12-11 14:49:44.772 31393-31393/com.example.momo.hasnavigationbar I/Nj: 华为手机 :false
网友评论