效果图

切换到桌面

切换到任务列表

解析
按下主页键会回到桌面,按下任务键会打开任务列表,这两个操作并未提供相应的按键处理方法,而是通过广播发出事件信息。
若想知晓是否回到桌面,以及是否打开任务列表,均需收听系统广播Intent.ACTION_CLOSE_SYSTEM_DIALOGS。
从收到的广播意图中获取原因reason字段,该字段值为homekey时表示回到桌面,值为recentapps时表示打开任务列表。
代码演示
package com.example.androidandroiddemo;
import androidx.appcompat.app.AppCompatActivity;
import android.app.PictureInPictureParams;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.util.Rational;
public class MainActivity extends AppCompatActivity {
private DeskoopRecevier deskoopRecevier;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
deskoopRecevier = new DeskoopRecevier();
IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
registerReceiver(deskoopRecevier,filter);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(deskoopRecevier);
}
//在进入画中画模式或退出画中画模式时模式时出发
@Override
public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode, Configuration newConfig) {
super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
if(isInPictureInPictureMode){
Log.d("hu","进入画中画模式");
}else {
Log.d("hu","推出画中画模式");
}
}
private class DeskoopRecevier extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
//表示回到桌面或切换到任务列表
if(intent != null && intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)){
String reson = intent.getStringExtra("reason");
if(!TextUtils.isEmpty(reson)&&
(reson.equals("homekey") || reson.equals("recentapps"))){
//Android 8.0开始才提供画中画模式
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
&& !isInPictureInPictureMode()){
//创建画中画模式的参数构建器
PictureInPictureParams.Builder builder = new PictureInPictureParams.Builder();
//设置小窗宽高的比例值,第一个参数表示分子,第二个参数表示分母
//下面的10/5=2,表示画中画窗口的宽度是高度的两倍
Rational rational = new Rational(10,5);
builder.setAspectRatio(rational);
//进入画中画模式
enterPictureInPictureMode(builder.build());
}
}
}
}
}
}
注意:
注册广播监听器和注销监听器要在onCreaty和onDestory里面而不是在onStart和onstoplim因为按home键时会执行onstop,我们在onstop时也要接收系统广播
配置清单文件:
设置为支持画中画模式
添加android:supportsPictureInPicture="true"
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AndroidAndroidDemo"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
android:supportsPictureInPicture="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
</application>
</manifest>