![](/img/trans.png)
[英]How to override animation when entering in PIP mode in android
[英]Entering in PIP Mode with the application minimized (Android Studio)
我的应用程序调用服务器可能需要几秒钟。 用户可以在收到服务器响应之前最小化应用程序。
所以我想做的是,如果应用程序在收到服务器响应时最小化,它将 go 到 PIP 模式。
当我尝试在应用程序最小化的情况下进入 Pip 模式时,出现以下错误:
java.lang.IllegalStateException: Activity must be resumed to enter picture-in-picture
执行此行时发生错误:
enterPictureInPictureMode();
谢谢。
进入模式前需要检查是否给予Picture-in-Picture
的特殊权限。
AppOpsManager manager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
if (manager != null) {
int modeAllowed = manager.unsafeCheckOpNoThrow(OPSTR_PICTURE_IN_PICTURE, Process.myUid(),
context.getPackageName());
if (modeAllowed == AppOpsManager.MODE_ALLOWED) {
// Enter picture-in-picture
}
}
如果您使用的是 rn-android-pip 库:
import android.app.AppOpsManager;
import android.content.Context;
import android.os.Process;
public void enterPictureInPictureMode() {
if (isPipSupported) {
AppOpsManager manager = (AppOpsManager) reactContext.getSystemService(Context.APP_OPS_SERVICE);
if (manager != null) {
int modeAllowed = manager.checkOpNoThrow(AppOpsManager.OPSTR_PICTURE_IN_PICTURE, Process.myUid(),
reactContext.getPackageName());
if (modeAllowed == AppOpsManager.MODE_ALLOWED) {
if (isCustomAspectRatioSupported) {
PictureInPictureParams params = new PictureInPictureParams.Builder()
.setAspectRatio(this.aspectRatio).build();
getCurrentActivity().enterPictureInPictureMode(params);
} else {
getCurrentActivity().enterPictureInPictureMode();
}
}
}
}
}
根据文档:
此外,指定您的活动处理布局配置更改,以便在 PiP 模式转换期间发生布局更改时您的活动不会重新启动。
切换到 PiP 模式会触发配置更改,这会导致重新创建 Activity,表示经历停止 -> 销毁 -> 创建 ->...
而我们可以找到Activity是如何决定自己是否可以进入PiP模式的: https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/java/android/app/Activity。 java
final void performStop(boolean preserveWindow, String reason) {
...
// Disallow entering picture-in-picture after the activity has been stopped
mCanEnterPictureInPicture = false;
...
}
public boolean enterPictureInPictureMode(@NonNull PictureInPictureParams params) {
...
if (!mCanEnterPictureInPicture) {
throw new IllegalStateException("Activity must be resumed to enter"
+ " picture-in-picture");
}
...
mIsInPictureInPictureMode = ActivityClient.getInstance().enterPictureInPictureMode(
mToken, params);
return mIsInPictureInPictureMode;
}
因此,您可能需要再次检查 AndroidManifest 以确保您已正确处理配置更改以避免 Activity 重新创建:
<activity android:name="VideoActivity"
android:supportsPictureInPicture="true"
android:configChanges=
"screenSize|smallestScreenSize|screenLayout|orientation"
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.