问题
The new ActivityOptions setLaunchDisplayId (int launchDisplayId) function in Android-O seems to always crash my app when I try to launch an activity intent.
Both when I launch activities from my own app and when I try to launch other apps i.e. Chrome Canary.
Does anyone know if this is a general problem with the new API's or am I missing something:
A small snippet of my code is below:
options.setLaunchDisplayId(1);
startActivity(intent, options);
NOTE I was testing with 'simulate a second screen' enabled (@1080p if it matters).
UPDATE
I have tried the ADB command adb shell start com.chrome.canary --display 1
,
and I get the message:
start: must be root
回答1:
I have connected to the second screen via the new API's with the code below, but as of yet have no way of interacting with it.
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);
}
回答2:
Here's a simplified answer which is working for me, built off of @Smiler. I tested this via a Button
click in my MainActivity
. Thanks for the help!
((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());
});
The Intent.FLAG_ACTIVITY_NEW_TASK
is extremely important in order to have the new application launch on a different display. Otherwise it'll launch in the same task stack, on the same display.
That 'new application' needs to also have android:resizeableActivity="true"
in the manifest. True
is the default, but I specified to just be explicit and protect future framework changes.
<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">
回答3:
each time you add a new simulate screen the display id changes. it resets to 1 when you restart. check if this is the issue. adb command that you specified doesnt seem to work as you said.
来源:https://stackoverflow.com/questions/42971442/android-o-launch-on-secondary-display