我创建了一个虚拟显示器并在其中打开了设置。如果我在设置中打开了一些其他活动,例如:wifi 页面。然后我按下主页按钮并返回最后打开的活动应该在虚拟显示器中。
现在,它即将到来的空白页。请指导在已创建的虚拟显示中检索上次打开的活动。
public class MainActivity extends AppCompatActivity {
private static final String TAG = "Test";
private static final int SCREEN_CAPTURE_PERMISSION_CODE = 1;
private int mWidth;
private int mHeight;
private DisplayMetrics mMetrics = new DisplayMetrics();
private DisplayManager mDisplayManager;
private VirtualDisplay mVirtualDisplay;
private int mDisplayId;
private MediaProjectionManager mProjectionManager;
private MediaProjection mProjection = null;
private int mResultCode;
private Intent mResultData;
private SurfaceView mSurfaceView;
private Surface mSurface;
private Display display;
int inputSource = InputDevice.SOURCE_UNKNOWN;
private Button mButtonA;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG2,"onCreate");
setContentView(R.layout.activity_main);
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG2,"onStart");
mSurfaceView = findViewById(R.id.surfaceView);
mSurface = mSurfaceView.getHolder().getSurface();
mWidth = mSurfaceView.getLayoutParams().width;
mHeight = mSurfaceView.getLayoutParams().height;
mButtonA = findViewById(R.id.mButtonA);
mDisplayManager = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
mProjectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
/* Just after virtual display is created*/
mSurfaceView.setOnTouchListener((v, event) -> {
inputSource = getSource(inputSource, InputDevice.SOURCE_TOUCHSCREEN);
injectMotionEvent(/*InputDevice.SOURCE_TOUCHSCREEN*/
inputSource, event.getAction(), event.getDownTime(), event.getEventTime(),
event.getX(), event.getY(), event.getPressure(), mDisplayId);
return true;
});
if(mResultData == null){
Log.d(TAG,"inside mResultdata null");
startActivityForResult(mProjectionManager.createScreenCaptureIntent(), SCREEN_CAPTURE_PERMISSION_CODE);
}else {
Log.d(TAG,"mVirtualdisplay.getDisplay" + mVirtualDisplay.getDisplay());
Log.d(TAG,"mVirtualdisplay.getSurface" + mVirtualDisplay.getSurface());
Log.d(TAG,"mvirtualdisplay.getDeviceid" + mVirtualDisplay.getDisplay().getDisplayId());
Log.d(TAG,"mSurface in else onstart:" + mSurface);
mVirtualDisplay.setSurface(mSurface);
mDisplayId = mVirtualDisplay.getDisplay().getDisplayId();
ActivityOptions options = ActivityOptions.makeBasic().setLaunchDisplayId(mDisplayId);
Intent intent = getPackageManager().getLaunchIntentForPackage("com.android.settings");
startActivity(intent,options.toBundle());
Log.d(TAG,"inside mResultData not null");
}
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onpause");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy");
if (mProjection != null) {
Log.i(TAG, "Stop media projection");
mProjection.stop();
mProjection = null;
destroyVirtualDisplay();
}
}
private void createVirtualDisplay() {
if (mProjection != null) {
Log.d(TAG, "createVirtualDisplay WxH (px): " + mWidth + "x" + mHeight + ", dpi: " + mMetrics.densityDpi);
String mVirtualDisplayTag = "TestVirtualDisplay";
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
display = wm.getDefaultDisplay();
display.getMetrics(mMetrics);
mVirtualDisplay = mProjection.createVirtualDisplay(mVirtualDisplayTag, mWidth, mHeight, mMetrics.densityDpi, 0, mSurface, null, null);
mVirtualDisplay.setSurface(mSurface);
Log.d(TAG,"mVirtualdisplaygetsurface in createvirtualdisplay" + mVirtualDisplay.getSurface());
mDisplayId = mVirtualDisplay.getDisplay().getDisplayId();
ActivityOptions options = ActivityOptions.makeBasic().setLaunchDisplayId(mDisplayId);
Intent intent = getPackageManager().getLaunchIntentForPackage("com.android.settings");
if(intent != null) {
Log.d(TAG2," inside intent is not null");
startActivity(intent, options.toBundle());
}
else {
Log.e(TAG2,"Can't start activity, since launch intent null");
}
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mResultCode = resultCode;
mResultData = data;
if (mProjection == null){
mProjection = mProjectionManager.getMediaProjection(mResultCode, mResultData);
}
createVirtualDisplay();
}
private void destroyVirtualDisplay() {
Log.d(TAG, "destroyVirtualDisplay");
if (mVirtualDisplay != null) {
Log.d(TAG, "destroyVirtualDisplay release");
mVirtualDisplay.release();
mVirtualDisplay = null;
}
}