Android virtual display release does not remove di

2019-08-14 06:59发布

问题:

I am using android Virtual Display along with Projection Manager At certain time, the projection need to be stopped, hence the following actions are taken

@Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        mMediaProjection.stop();
        Log.e(TAG, "mMediaProjection.stopped ");
        mVirtualDisplay.release();
        Log.e(TAG, "virtual display.released ");
    }

The surfaceDestroyed is related to the surface which backs the Virtual Display.

When it is the time to start Media Projection again, if available displays are iterated, the previously created virtual display still listed as one of available displays

DisplayManager disp = (DisplayManager) getSystemService(DISPLAY_SERVICE);
        Display[] allDisplays = disp.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION);
        Log.e(TAG+"-Display", "Display Count  "+allDisplays.length);
        for(Display dl : allDisplays){
            Log.e(TAG+"-Display", "Display name " + dl.getName()+" Display id "+dl.getDisplayId());
        }

How to properly handle Virtual display creation and removal ?

Note :

Following is the snippet of media projection and virtual display setup

mMediaProjection = mMediaProjectionManager.getMediaProjection(mResultCode, mResultData);
mVirtualDisplay = mMediaProjection.createVirtualDisplay("ScreenCapture",
                mSurfaceView.getWidth(), mSurfaceView.getHeight(), mScreenDensity,
                 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC | DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR |DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION,
                mSurface, null, null);