下面列出了怎么用android.hardware.display.DisplayManager的API类实例代码及写法,或者点击链接到github查看源代码。
public OverlayDisplayWindow(Context context, String name,
int width, int height, int densityDpi, int gravity, boolean secure,
Listener listener) {
// Workaround device freeze (b/38372997)
ThreadedRenderer.disableVsync();
mContext = context;
mName = name;
mGravity = gravity;
mSecure = secure;
mListener = listener;
mDisplayManager = (DisplayManager)context.getSystemService(
Context.DISPLAY_SERVICE);
mWindowManager = (WindowManager)context.getSystemService(
Context.WINDOW_SERVICE);
mDefaultDisplay = mWindowManager.getDefaultDisplay();
updateDefaultDisplayInfo();
resize(width, height, densityDpi, false /* doLayout */);
createWindow();
}
@Override // Binder call
public int applyVirtualDisplayFlags(int flags) {
if (mType == MediaProjectionManager.TYPE_SCREEN_CAPTURE) {
flags &= ~DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY;
flags |= DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR
| DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION;
return flags;
} else if (mType == MediaProjectionManager.TYPE_MIRRORING) {
flags &= ~(DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR);
flags |= DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY |
DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION;
return flags;
} else if (mType == MediaProjectionManager.TYPE_PRESENTATION) {
flags &= ~DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY;
flags |= DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION |
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
return flags;
} else {
throw new RuntimeException("Unknown MediaProjection type");
}
}
private boolean isScreenOn() {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
DisplayManager dm = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
boolean screenOn = false;
for (Display display : dm.getDisplays()) {
if (display.getState() != Display.STATE_OFF) {
screenOn = true;
}
}
return screenOn;
} else {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
//noinspection deprecation
return pm.isScreenOn();
}
}
public void onResume() {
if (mVirtualDisplay == null) {
DisplayManager manager = (DisplayManager) mContext.getSystemService(Context.DISPLAY_SERVICE);
Display defaultDisplay = manager.getDisplay(Display.DEFAULT_DISPLAY);
int flags = DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY;
defaultDisplay.getMetrics(mDefaultMetrics);
mVirtualDisplay = manager.createVirtualDisplay("OffscreenViews", mWidth, mHeight,
mDefaultMetrics.densityDpi, mSurface, flags);
}
if (mPresentation == null) {
mPresentation = new OffscreenPresentation(mContext, mVirtualDisplay.getDisplay());
mPresentation.show();
if (mContentView != null) {
mPresentation.setContentView(mContentView);
}
}
}
public Bitmap capture() {
try {
mVirtualDisplay = mMediaProjection.createVirtualDisplay(TAG, windowWidth, windowHeight, mScreenDensity, //
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,//
mImageReader.getSurface(), null, null);
Log.i(TAG, "screen capture completed");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
return createBitmap();
} finally {
release();
}
}
/**
* 将录屏服务的内容显示到Image面板上.
*/
private void initVirtualDisplay() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
DisplayManager mDisplayManager = (DisplayManager) this.getSystemService(Context.DISPLAY_SERVICE);
if (mDisplayManager == null) {
L.e(" 获取 播放管理器失败 ... ");
}
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
L.d("当前安卓 版本 " + Build.VERSION.SDK_INT + " 进入到图片展示 ");
mVirtualDisplay = mDisplayManager.createVirtualDisplay("Remote Droid", mScreenWidth,
mScreenHeight, mScreenDensity,
mImageReader.getSurface(),
DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC | DisplayManager.VIRTUAL_DISPLAY_FLAG_SECURE);
}
} else {
if (mMediaProjection == null) {
L.e("mMediaProjection is null ...");
return;
} else {
mVirtualDisplay = mMediaProjection.createVirtualDisplay("screen-mirror",
mScreenWidth, mScreenHeight, mScreenDensity, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
mImageReader.getSurface(), null, null);
}
}
}
/**
* Is the screen of the device on.
*
* @param context the context
* @return true when (at least one) screen is on
*/
public boolean isScreenOn(Context context) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
DisplayManager dm = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
boolean screenOn = false;
for (Display display : dm.getDisplays()) {
if (display.getState() != Display.STATE_OFF) {
screenOn = true;
}
}
return screenOn;
} else {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
return pm.isScreenOn();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tb_activity_hsl_config);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
TextView textView = findViewById(R.id.textView);
textView.setText(R.string.tb_desktop_mode_is_active);
DisplayManager manager = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
manager.registerDisplayListener(listener, null);
U.registerReceiver(this, unDimScreenReceiver, ACTION_UNDIM_SCREEN);
U.registerReceiver(this, finishReceiver,
ACTION_FINISH_DIM_SCREEN_ACTIVITY, ACTION_KILL_HOME_ACTIVITY);
if(getSupportActionBar() == null) return;
// Make action bar invisible
getSupportActionBar().setElevation(0);
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(0));
setTitle(null);
}
private void setUpVirtualDisplay() {
Log.i(TAG, "Setting up a VirtualDisplay: " +
mSurfaceView.getWidth() + "x" + mSurfaceView.getHeight() +
" (" + mScreenDensity + ")");
mVirtualDisplay = mMediaProjection.createVirtualDisplay("ScreenCapture",
mSurfaceView.getWidth(), mSurfaceView.getHeight(), mScreenDensity,
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
mSurface, null, null);
mButtonToggle.setText(R.string.stop);
}
public JellybeanMr1Impl(Context context, String name,
int width, int height, int gravity) {
super(context, name, width, height, gravity);
mDisplayManager = (DisplayManager)context.getSystemService(
Context.DISPLAY_SERVICE);
mWindowManager = (WindowManager)context.getSystemService(
Context.WINDOW_SERVICE);
mDefaultDisplay = mWindowManager.getDefaultDisplay();
updateDefaultDisplayInfo();
createWindow();
}
public ActiveScanWorkaround(Context context, Handler handler) {
if (Build.VERSION.SDK_INT != 17) {
throw new UnsupportedOperationException();
}
mDisplayManager = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
mHandler = handler;
try {
mScanWifiDisplaysMethod = DisplayManager.class.getMethod("scanWifiDisplays");
} catch (NoSuchMethodException ex) {
}
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
DisplayManager dpyMgr =
(DisplayManager) getContext().getSystemService(Context.DISPLAY_SERVICE);
dpyMgr.registerDisplayListener(mDisplayListener, new Handler(Looper.getMainLooper()));
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
DisplayManager dpyMgr =
(DisplayManager) getContext().getSystemService(Context.DISPLAY_SERVICE);
dpyMgr.unregisterDisplayListener(mDisplayListener);
}
boolean isScreenOn() {
if (Utils.isAndroidNewerThanL()) {
DisplayManager dm = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
for (Display display : dm.getDisplays()) {
if (display.getState() != Display.STATE_OFF) {
return true;
}
}
return false;
} else {
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
return powerManager.isScreenOn();
}
}
public JellybeanMr1Impl(Context context, String name,
int width, int height, int gravity) {
super(context, name, width, height, gravity);
mDisplayManager = (DisplayManager)context.getSystemService(
Context.DISPLAY_SERVICE);
mWindowManager = (WindowManager)context.getSystemService(
Context.WINDOW_SERVICE);
mDefaultDisplay = mWindowManager.getDefaultDisplay();
updateDefaultDisplayInfo();
createWindow();
}
private void initVirtualDisplay() {
mSurface = super.getmSurface();
mVirtualDisplay = mMediaProjection.createVirtualDisplay(TAG + "-display",
mWidth, mHeight, mDpi, DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC,
mSurface, null, null);
Log.d(TAG, "created virtual display: " + mVirtualDisplay);
}
public Object getService(ContextImpl ctx) {
Display display = ctx.mDisplay;
if (display == null) {
if (mDefaultDisplay == null) {
DisplayManager dm = (DisplayManager)ctx.getOuterContext().
getSystemService(Context.DISPLAY_SERVICE);
mDefaultDisplay = dm.getDisplay(Display.DEFAULT_DISPLAY);
}
display = mDefaultDisplay;
}
return new WindowManagerImpl(display);
}
private VirtualDisplay createVirtualDisplay() {
int w = mWidth;
int h = mHeight;
WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
if (wm == null) {
throw new RuntimeException("WindowManager is not supported.");
}
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
if (dm.widthPixels > dm.heightPixels) {
if (w < h) {
w = mHeight;
h = mWidth;
}
} else {
if (w > h) {
w = mHeight;
h = mWidth;
}
}
return mMediaProjection.createVirtualDisplay(
"Android Host Screen",
w,
h,
mDisplayDensityDpi,
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
getSurface(),
mCallback,
new Handler(Looper.getMainLooper()));
}
public Object getService(ContextImpl ctx) {
Display display = ctx.mDisplay;
if (display == null) {
DisplayManager dm = (DisplayManager)ctx.getOuterContext().getSystemService(
Context.DISPLAY_SERVICE);
display = dm.getDisplay(Display.DEFAULT_DISPLAY);
}
return new WindowManagerImpl(display);
}
public Object getService(ContextImpl ctx) {
Display display = ctx.mDisplay;
if (display == null) {
DisplayManager dm = (DisplayManager)ctx.getOuterContext().getSystemService(
Context.DISPLAY_SERVICE);
display = dm.getDisplay(Display.DEFAULT_DISPLAY);
}
return new WindowManagerImpl(display);
}
public MiracastDisplayListener(int currentDisplay, RemoteViews widgetRemoteViews, DisplayManager displayManager, AppWidgetManager appWidgetManager, int appWidgetId, Context context){
mCurrentDisplay = currentDisplay;
mViews = widgetRemoteViews;
mDisplayManager = displayManager;
mAppWidgetManager = appWidgetManager;
mAppWidgetId = appWidgetId;
mContext = context;
MiracastApplication application
= (MiracastApplication) mContext.getApplicationContext();
mTracker = application.getDefaultTracker();
}
public Object getService(ContextImpl ctx) {
Display display = ctx.mDisplay;
if (display == null) {
if (mDefaultDisplay == null) {
DisplayManager dm = (DisplayManager)ctx.getOuterContext().
getSystemService(Context.DISPLAY_SERVICE);
mDefaultDisplay = dm.getDisplay(Display.DEFAULT_DISPLAY);
}
display = mDefaultDisplay;
}
return new WindowManagerImpl(display);
}
@Override
public void onDisplayRemoved(int displayId) {
DisplayManager dm = (DisplayManager) getSystemService(DISPLAY_SERVICE);
Display[] displays = dm.getDisplays();
try {
if(displays[displays.length - 1].getDisplayId() == Display.DEFAULT_DISPLAY) {
Intent serviceIntent = new Intent(NotificationService.this, TempBacklightOnService.class);
U.startService(NotificationService.this, serviceIntent);
SharedPreferences prefMain = U.getPrefMain(NotificationService.this);
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
boolean displayConnectionServiceRunning = false;
for(ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if(DisplayConnectionService.class.getName().equals(service.service.getClassName()))
displayConnectionServiceRunning = true;
}
if(prefMain.getBoolean("inactive", true) && !displayConnectionServiceRunning) {
Intent turnOffIntent = new Intent(NotificationService.this, TurnOffActivity.class);
turnOffIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(turnOffIntent);
}
}
} catch (ArrayIndexOutOfBoundsException e) { /* Gracefully fail */ }
}
/**
* Basic constructor.
*
* @param ctxt a Context, typically the activity that is planning on showing
* the Presentation
* @param listener the callback for show/hide events
*/
public PresentationHelper(Context ctxt, Listener listener) {
this.listener=listener;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
mgr=
(DisplayManager)ctxt.getSystemService(Context.DISPLAY_SERVICE);
}
}
public static boolean isScreenOn(Context context) {
DisplayManager dm = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
for (Display display : dm.getDisplays()) {
if (display.getState() == Display.STATE_ON
|| display.getState() == Display.STATE_UNKNOWN) {
return true;
}
}
return false;
}
public static void init() {
sMediaProjection = mProjectionManager.getMediaProjection(resultCode, data);
//start capture reader
mImageReader = ImageReader.newInstance(MainApplication.sceenWidth, MainApplication.sceenHeight,
PixelFormat.RGBA_8888, 2);
mVirtualDisplay = sMediaProjection.createVirtualDisplay(
"ScreenShot",
MainApplication.sceenWidth,
MainApplication.sceenHeight,
MainApplication.dpi,
DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY | DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC,
mImageReader.getSurface(),
null,
null);
// mImageReader.setOnImageAvailableListener(reader -> {
// Bitmap bitmap = null;
// try (android.media.Image image = reader.acquireLatestImage()) {
// if (image != null) {
// bitmap = covetBitmap(image);
//// if (ScreenCaptureUtil.screenCache != null) {
//// ScreenCaptureUtil.screenCache.recycle();
//// }
// ScreenCaptureUtil.screenCache = bitmap;
// }
// } catch (Exception e) {
// e.printStackTrace();
// }
//
// }, getBackgroundHandler());
}
private static DisplayMetrics getEPDDisplayMetrics() {
Context context = AppUtils.getAppContext();
DisplayManager displayManager = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
Display[] presentationDisplays = displayManager.getDisplays();
Display display;
if (presentationDisplays.length > 1) {
display = presentationDisplays[1];
} else {
display = presentationDisplays[0];
}
DisplayMetrics metrics = new DisplayMetrics();
display.getRealMetrics(metrics);
LogUtils.d("widthPixels=" + metrics.widthPixels + " heightPixels=" + metrics.heightPixels);
return metrics;
}
public static boolean isScreenOn(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
DisplayManager dm = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
for (Display display : dm.getDisplays()) {
if (display.getState() != Display.STATE_OFF) {
return true;
}
}
return false;
} else {
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
return powerManager.isScreenOn();
}
}
private boolean isDisplayOn(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
DisplayManager dm = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
for (Display display : dm.getDisplays()) {
if (display.getState() != Display.STATE_OFF) {
return true;
}
}
return false;
} else {
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
return powerManager.isScreenOn();
}
}
private void start(Activity activity, int resultCode, Intent data) {
if (mediaProjectionManager == null) {
return;
}
startTime = System.currentTimeMillis();
try {
mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, data);
DisplayMetrics metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenWidth = metrics.widthPixels;
int screenHeight = metrics.heightPixels;
int density = metrics.densityDpi;
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setOutputFile(getSaveFile());
mediaRecorder.setVideoSize(screenWidth, screenHeight);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
mediaRecorder.setVideoEncodingBitRate(screenWidth * screenHeight);
mediaRecorder.setVideoFrameRate(30);
mediaRecorder.prepare();
display = mediaProjection.createVirtualDisplay(QPMScreenRecorderManager.class.getSimpleName(),
screenWidth, screenHeight, density, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
mediaRecorder.getSurface(), null, null);
mediaRecorder.start();
} catch (Exception e){
e.printStackTrace();
onRecorderFailed(activity);
}
}