You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中onPictureInPictureModeChanged无法检测PIP模式的问题求助

Android PIP模式切换检测问题解答

场景与代码

我正在测试Android的PIP模式,需求是检测PIP模式切换并执行相应操作。以下是测试代码:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <VideoView
        android:id="@+id/videoView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <Button
        android:id="@+id/pipButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="33dp"
        android:onClick="enterPipMode"
        android:text="@string/enter_pip_mode"
        app:layout_constraintBottom_toBottomOf="@+id/videoView1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity关键代码

@Override
public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
    super.onPictureInPictureModeChanged(isInPictureInPictureMode);

    if(isInPictureInPictureMode) {
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.ebookfrenzy.videoplayer.VIDEO_INFO");

        receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Toast.makeText(context, "Favorite Home Movie Clips", Toast.LENGTH_LONG).show();
            }
        };
        registerReceiver(receiver, filter);
        createPipAction();
    } else {
        binding.videoView1.setMediaController(mediaController);
        binding.pipButton.setVisibility(View.VISIBLE);
        if(receiver != null) {
            unregisterReceiver(receiver);
        }
    }
}

问题现象

切换到PIP模式时,无参数的onPictureInPictureModeChanged方法未被调用;但添加Configuration参数后方法可正常触发:

  • 无效写法:
    public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode)
    
  • 有效写法:
    public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode, Configuration config)
    

疑问解答

  1. 能否用无参数的写法A检测PIP切换?
    不行。无参数的onPictureInPictureModeChanged是API 26引入的旧方法,在API 28及以后的版本中,系统只会调用带Configuration参数的重载方法。Android Studio不报错是因为这个方法仍然存在于SDK中,但系统不会再触发它,所以没法用写法A实现检测。

  2. 是否必须使用写法B?
    是的,如果你需要适配API 28及以上的系统,必须使用带Configuration参数的重载方法。如果你的App最低兼容版本低于API 28,可以同时实现两个重载方法:低版本系统会调用无参数的,高版本调用带参数的,但目前主流设备都已经是API 28以上,所以优先实现写法B即可。

  3. Configuration是什么?
    Configuration是Android系统用来描述设备当前配置信息的类,包含屏幕方向、分辨率、语言、字体缩放、是否处于PIP模式等一系列设备和应用的配置状态。在PIP模式切换时,系统会把切换后的最新配置信息传入这个参数,你可以通过它获取更多和当前模式相关的细节,比如PIP窗口的大小、屏幕方向变化等。


内容的提问来源于stack exchange,提问作者passion053

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 03:49:54