如何在虚拟屏幕上启动Android应用程序?

问题描述 投票:0回答:2

目标

我想运行Android应用程序(第三方),但不将其显示在物理屏幕上。

条件

  • 拥有完整的系统源码(AOSP)
  • 有root权限

尝试

  • 尝试创建一个虚拟屏幕,并请求应用程序显示在虚拟屏幕上(以下代码运行时带有
    android.uid.system
    。)
    public static android.hardware.display.VirtualDisplay createDisplay(
            Context context,
            Surface surface,
            String name,
            int height,
            int width,
            int dpi
    ) {
        DisplayManager displayManager = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);

        android.hardware.display.VirtualDisplay display = displayManager.createVirtualDisplay(
                name, width, height, dpi, surface,
                DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
                        DisplayManager.VIRTUAL_DISPLAY_FLAG_SECURE |
                        DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY |
                        DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION |
                        DisplayManager.VIRTUAL_DISPLAY_FLAG_ROTATES_WITH_CONTENT |
                        DisplayManager.VIRTUAL_DISPLAY_FLAG_TRUSTED |
                        DisplayManager.VIRTUAL_DISPLAY_FLAG_SUPPORTS_TOUCH |
                        /*DisplayManager. VIRTUAL_DISPLAY_FLAG_OWN_FOCUS*/ 1 << 14,
        );

        return display;
    }

    public static void startApplication(Context context, Display display, String packageName) {
        Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName)
                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                .addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);

        ActivityOptions options = ActivityOptions.makeBasic()
                .setLaunchDisplayId(display.getDisplayId());

        context.startActivity(intent, options.toBundle());
    }

但是应用程序会显示在真实屏幕上。


我错过了什么吗?

如何让指定的应用程序只出现在虚拟显示器上,而不出现在物理屏幕上?

android android-source android-virtualdisplay
2个回答
0
投票

从输出层删除应用程序的表面很容易。


0
投票

下面正在工作。

 void startActivity(Surface mySurface){
    // Get the DisplayManager
    DisplayManager displayManager = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
    DisplayMetrics displayMetrics = getResources().getDisplayMetrics();

    virtualDisplay = displayManager.createVirtualDisplay(
            "MyVirtualDisplay", // Name of the virtual display
            1920, 1280, // Width and height
            displayMetrics.densityDpi, // Screen density
            mySurface, // Surface for the virtual display
            DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC // Flags
    );

    // Identify the presentation display based on your criteria, e.g., display ID
    Display presentationDisplay = null;
    presentationDisplay = virtualDisplay.getDisplay();



    if (presentationDisplay != null) {
        // Create an Intent to launch the app by its package name
        String packageName = "com.mypackage.name"; // Replace with the actual package name of the app you want to open
        Intent intent = getPackageManager().getLaunchIntentForPackage(packageName);

        if (intent != null) {
            // Set the target display for the intent (presentation display)
            int displayId = virtualDisplay.getDisplay().getDisplayId();
            ActivityOptions options = ActivityOptions.makeBasic().setLaunchDisplayId(displayId);
            intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT | Intent.FLAG_ACTIVITY_NEW_TASK);

            startActivity(intent, options.toBundle());
        } else {
            // The app with the specified package name is not installed on the device
            // Handle this case accordingly
        }
    } else {
        // Presentation display not found or criteria not met
        // Handle this case accordingly
    }

}

从下面的方法调用:

void createVirtualDisplay(){
    SurfaceView surfaceView = findViewById(R.id.my_surface_view);
    SurfaceHolder holder = surfaceView.getHolder();
    holder.addCallback(new SurfaceHolder.Callback() {
        @RequiresApi(api = Build.VERSION_CODES.O)
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            // You can use the holder.getSurface() here
            Surface mySurface = holder.getSurface();
            startActivity(mySurface);
        }

        @Override
        public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {

        }

        @Override
        public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {

        }

        // Implement other callback methods
    });
}

我不知道如何对其应用交集。所以最好有人添加更多细节。

谢谢,

© www.soinside.com 2019 - 2024. All rights reserved.