如何在android N(SDK 24)的分屏中打开另一个应用程序?
在文档中我发现了这个:
多窗口模式启动新 Activity
当您启动一个新 Activity 时,您可以向系统提示新 Activity 应该显示在当前 Activity 旁边,如果可能的话。为此,请使用标志 Intent.FLAG_ACTIVITY_LAUNCH_TO_ADJACENT。传递此标志请求以下行为:
如果设备处于分屏模式,系统会尝试在启动它的 Activity 旁边创建新的 Activity,因此这两个 Activity 共享屏幕。系统不能保证能够做到这一点,但如果可能的话,它会使 Activity 相邻。
如果设备未处于分屏模式,则此标志无效。
如果设备处于自由模式并且您正在启动一个新 Activity ,您可以通过调用 ActivityOptions.setLaunchBounds() 来指定新 Activity 的尺寸和屏幕位置。如果设备不处于多窗口模式,此方法无效。
所以当我尝试这个时,Intent.FLAG_ACTIVITY_LAUNCH_TO_ADJACENT
标志不存在。我安装了
这是我的毕业生:
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.0"
defaultConfig {
applicationId "PACKAGENAME"
minSdkVersion 16
targetSdkVersion 24
versionCode 2
versionName "2.4.1"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.0.0'
compile 'com.android.support:design:24.0.0'
compile 'org.jsoup:jsoup:1.8.3'
compile 'com.android.support:support-v4:24.0.0'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.okhttp3:okhttp:3.2.0'
}
最佳答案
您好从 android 7.0 开始,您可以进入自由格式多窗口模式。在您的设备中打开开发人员选项并使用以下 adb 命令:
adb shell settings put global enable_freeform_support 1
adb shell settings put global force_resizable_activities 1
在您可以使用以下功能以编程方式在分屏中启动不同的 Activity 之后:private void goToSplitMode() {
Intent i = new Intent(this, SplitMainActivity.class);
PackageManager manager = getPackageManager();
i = manager.getLaunchIntentForPackage("com.google.android.apps.maps");
i.addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT |
Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
DisplayMetrics displayMetrics = new DisplayMetrics();
Rect mBounds = new Rect(300, 0, getScreenWidth(this), getScreenHeight(this));
mOptions = getActivityOptions(MainActivity.this);
mOptions = mOptions.setLaunchBounds(mBounds);
startActivity(i, mOptions.toBundle());
i = new Intent(this, SplitMainActivity.class);
mBounds = new Rect(0, 0, 300, getScreenHeight(this));
mOptions = getActivityOptions(MainActivity.this);
mOptions = mOptions.setLaunchBounds(mBounds);
i.addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT |
Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
startActivity(i, mOptions.toBundle());
}
public static ActivityOptions getActivityOptions(Context context) {
ActivityOptions options = ActivityOptions.makeBasic();
int freeform_stackId = 5;
try {
Method method = ActivityOptions.class.getMethod("setLaunchWindowingMode", int.class);
method.invoke(options, freeform_stackId);
} catch (Exception e) { /* Gracefully fail */
}
return options;
}
public static int getScreenWidth(@NonNull Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
WindowMetrics windowMetrics = activity.getWindowManager().getCurrentWindowMetrics();
Insets insets = windowMetrics.getWindowInsets()
.getInsetsIgnoringVisibility(WindowInsets.Type.systemBars());
return windowMetrics.getBounds().width() - insets.left - insets.right;
} else {
DisplayMetrics displayMetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
return displayMetrics.widthPixels;
}
}
public static int getScreenHeight(@NonNull Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
WindowMetrics windowMetrics = activity.getWindowManager().getCurrentWindowMetrics();
Insets insets = windowMetrics.getWindowInsets()
.getInsetsIgnoringVisibility(WindowInsets.Type.systemBars());
return windowMetrics.getBounds().height() - insets.top - insets.bottom;
} else {
DisplayMetrics displayMetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
return displayMetrics.heightPixels;
}
}
关于java - 以编程方式在分屏中打开应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37907094/