package 哈哈哈哈哈哈哈哈哈哈;
import android.content.Context; import android.graphics.Color; import android.os.Build; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.widget.TextView; import java.lang.reflect.Method; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv_pd = findViewById(R.id.tv_pd); StringBuffer sb = new StringBuffer(); boolean bb = hasNotchInScreen(this); int banben = Build.VERSION.SDK_INT; sb.append("代号(28)" + banben); sb.append("\n"); if (bb) { sb.append("是刘海屏幕" + bb); sb.append("\n"); int[] iwh = getNotchSize(this); sb.append("宽:" + iwh[0]); sb.append("\n"); sb.append("高:" + iwh[1]); sb.append("\n手机厂商一般情况下已经处理好刘海问题,仅在全屏下稍作考虑"); } else { sb.append("不是刘海屏幕" + bb); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { getWindow().setNavigationBarColor(Color.parseColor("#3F51B5")); //getWindow().setNavigationBarColor(getResources().getColor(R.color.black)); //getWindow().setNavigationBarColor(Color.BLUE); sb.append("修改虚拟按键背景颜色修改成功,按键颜色不变"); } tv_pd.setText(sb.toString()); } /** * 华为start */ //判断是否是华为刘海屏 public static boolean hasNotchInScreen(Context context) { boolean ret = false; try { ClassLoader cl = context.getClassLoader(); Class HwNotchSizeUtil = cl.loadClass("com.huawei.android.util.HwNotchSizeUtil"); Method get = HwNotchSizeUtil.getMethod("hasNotchInScreen"); ret = (boolean) get.invoke(HwNotchSizeUtil); } catch (ClassNotFoundException e) { Log.e("test", "hasNotchInScreen ClassNotFoundException"); } catch (NoSuchMethodException e) { Log.e("test", "hasNotchInScreen NoSuchMethodException"); } catch (Exception e) { Log.e("test", "hasNotchInScreen Exception"); } finally { return ret; } } //获取华为刘海的高宽 public static int[] getNotchSize(Context context) { int[] ret = new int[]{0, 0}; try { ClassLoader cl = context.getClassLoader(); Class HwNotchSizeUtil = cl.loadClass("com.huawei.android.util.HwNotchSizeUtil"); Method get = HwNotchSizeUtil.getMethod("getNotchSize"); ret = (int[]) get.invoke(HwNotchSizeUtil); } catch (ClassNotFoundException e) { Log.e("haha", "getNotchSize ClassNotFoundException"); } catch (NoSuchMethodException e) { Log.e("haha", "getNotchSize NoSuchMethodException"); } catch (Exception e) { Log.e("haha", "getNotchSize Exception"); } finally { return ret; } } }