Android-O 在辅助显示器上启动

Smi*_*ler 5 api user-interface android display android-8.0-oreo

当我尝试启动活动意图时,Android-O 中的新 ActivityOptions setLaunchDisplayId (int launchDisplayId) 函数似乎总是使我的应用程序崩溃。

当我从我自己的应用程序启动活动时,以及当我尝试启动其他应用程序(例如 Chrome Canary)时。

有谁知道这是新 API 的普遍问题还是我遗漏了什么:

我的代码的一小段如下:

options.setLaunchDisplayId(1); startActivity(intent, options);

注意 我正在测试启用“模拟第二个屏幕”(@1080p,如果重要的话)。

更新 我已经尝试了 ADB 命令adb shell start com.chrome.canary --display 1,我收到了消息:

开始:必须是root

Tod*_*and 6

这是一个对我有用的简化答案,基于@Smiler。我通过点击Button我的MainActivity. 谢谢您的帮助!

    ((Button) findViewById(R.id.launchTopScreen)).setOnClickListener(v -> {
        Intent intent = getPackageManager().getLaunchIntentForPackage("com.example.new.app");
        intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT | Intent.FLAG_ACTIVITY_NEW_TASK);
        ActivityOptions options = ActivityOptions.makeBasic().setLaunchDisplayId(1);
        startActivity(intent, options.toBundle());
    });
Run Code Online (Sandbox Code Playgroud)

Intent.FLAG_ACTIVITY_NEW_TASK为了在不同的显示器上启动新应用程序,这一点极其重要。否则,它将在同一显示器上的同一任务堆栈中启动。

该“新应用程序”也需要包含android:resizeableActivity="true"在清单中。True是默认值,但我指定为明确的并保护未来的框架更改。

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:resizeableActivity="true"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
Run Code Online (Sandbox Code Playgroud)


Smi*_*ler 5

我已经使用下面的代码通过新的 API 连接到第二个屏幕,但到目前为止还没有办法与之交互。

 Bundle bdl;
 MediaRouter mediaRouter = (MediaRouter) mContext.getSystemService(Context.MEDIA_ROUTER_SERVICE);
 MediaRouter.RouteInfo route = mediaRouter.getSelectedRoute(MediaRouter.ROUTE_TYPE_LIVE_VIDEO);
 if (route != null) {
     Display presentationDisplay = route.getPresentationDisplay();
     bdl = ActivityOptions.makeClipRevealAnimation(mView, left, top, width, height).setLaunchBounds(rect).setLaunchDisplayId(presentationDisplay.getDisplayId()).toBundle();
     Bundle optsBundle = true ? bdl : null;
     Intent intent = new Intent(mContext, SecondaryActivity.class);
     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     mContext.startActivity(intent, optsBundle);
 }
Run Code Online (Sandbox Code Playgroud)