android - 图片模式下进入图片时如何不分离任务
问题描述
我正在创建一个媒体播放器应用程序,我想支持画中画。我的问题是每当我输入 PIP 时,玩家活动都会与之前的活动等分开。
这是我的活动声明:
<activity
android:name=".ui.player.PlayerActivity"
android:allowTaskReparenting="true"
android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"
android:exported="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/player"
android:launchMode="singleTask"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsPictureInPicture="true">
这是我用来输入 PIP 的代码:
override fun onUserLeaveHint() {
enterPictureInPicture()
}
private fun enterPictureInPicture() {
val player = manager.currentPlayer
if (player is SimpleExoPlayer) {
val format = player.videoFormat
val params = PictureInPictureParams.Builder()
.setAspectRatio(format?.let { Rational(it.width, it.height) } ?: Rational(16, 9))
.build()
enterPictureInPictureMode(params)
}
}
解决方案
将 PIP 扩展回应用程序后,我遇到了运行多个任务的问题。我们使用多个活动,因为我们希望 PIP 在应用程序中运行。因此,当用户触发扩展/恢复 pip 时,我调用此方法:
public static boolean removeLauncherTask(Context appContext) {
ActivityManager activityManager = (ActivityManager) appContext.getSystemService(Context.ACTIVITY_SERVICE);
// iterate app tasks available and navigate to launcher task (browse task)
assert activityManager != null;
final List<ActivityManager.AppTask> appTasks = activityManager.getAppTasks();
for (ActivityManager.AppTask task : appTasks) {
final Intent baseIntent = task.getTaskInfo().baseIntent;
final Set<String> categories = baseIntent.getCategories();
if (categories != null && categories.contains(Intent.CATEGORY_LAUNCHER)) {
PictureInPictureActivity.mBackstackLost = true; // to keep track
task.setExcludeFromRecents(true);
return true;
}
}
return false;
}
在恢复 pip 之后,您仍然需要处理您的 backstack,但许多帖子已经解决了这个问题。提示如果您丢失了 backstack,则需要在从 pip 活动导航后调用此方法
public static boolean navToLauncherTask(Context appContext) {
ActivityManager activityManager = (ActivityManager) appContext.getSystemService(Context.ACTIVITY_SERVICE);
// iterate app tasks available and navigate to launcher task (browse task)
assert activityManager != null;
final List<ActivityManager.AppTask> appTasks = activityManager.getAppTasks();
for (ActivityManager.AppTask task : appTasks) {
final Intent baseIntent = task.getTaskInfo().baseIntent;
final Set<String> categories = baseIntent.getCategories();
if (categories != null && categories.contains(Intent.CATEGORY_LAUNCHER)) {
task.moveToFront();
return true;
}
}
return false;
}
''''
Which looks very similar as the removing of the main task.
推荐阅读
- c# - 如何在 .Net Core 中使用客户端 SSL 证书
- python - 如何从这些数据中绘制 ROC 曲线?
- wordpress - Wpbakery v5.5.2 后端编辑器出现空白页
- java - Android - 为每个按钮自动查找并创建一个 setOnClickListener
- c# - 异步执行代码并返回 MVC 动作
- reactjs - c# + reactjs + typescript + 基于令牌的身份验证的模板
- php - 使用 Abyss Web Server 实现 MySQL 时如何修复 HY000/2054?
- c# - 角色触墙后无法移动
- javascript - 将参数从我的函数传递到调用函数
- python-3.x - 如何使用 numpy 构建 RNN