// getDisplay() 在低于 API 30 为 @hide API,只能反射调用
1.kotlin代码
private fun getCurrentDisplay(): Display? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
display
} else {
// getDisplay() 在低于 API 30 为 @hide API,只能反射调用
runCatching {
val method: Method = this.javaClass.getMethod("getDisplay")
method.isAccessible = true
method.invoke(this) as? Display
}.getOrNull()
}
}
2.java 代码
private int getCurrentDisplayId() {
Context context = this(这个要求是 activity 的 Context!!!);
int currentDisplayId = 0;
try {
Method method = context.getClass().getMethod("getDisplay");
method.setAccessible(true);
Display display = (Display) method.invoke(context);
if (display != null) {
currentDisplayId = display.getDisplayId();
}
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
return currentDisplayId; }