7

当我尝试启动活动意图时,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,我得到了消息:

开始:必须是根

4

3 回答 3

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);
 }
于 2017-03-25T19:23:00.643 回答
4

这是一个对我有用的简化答案,基于@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());
    });

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">
于 2019-02-05T14:28:33.727 回答
0

每次添加新的模拟屏幕时,显示 ID 都会更改。重新启动时它会重置为 1。检查这是否是问题。您指定的 adb 命令似乎不像您说的那样工作。

于 2020-09-28T05:24:30.243 回答