![](/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.