首页 > 解决方案 > 图片模式下进入图片时如何不分离任务

问题描述

我正在创建一个媒体播放器应用程序,我想支持画中画。我的问题是每当我输入 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)
        }
    }

标签: androidkotlin

解决方案


将 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. 


推荐阅读