[null,null,["最后更新时间 (UTC):2025-08-21。"],[],[],null,["# Multitasking on TV\n\nAndroid 14 (API level 34) introduces some enhancements to the\n[picture-in-picture](/guide/topics/ui/picture-in-picture) (PiP) APIs to allow for multitasking. While PiP\nsupport was introduced in Android 8.0 (API level 26), it was not widely\nsupported on Android TV, and not supported at all on Google TV prior to Android\n13. Multitasking for TV uses PiP mode to allow two\nseparate apps to coexist on the screen: one running in full\nscreen, with a second running in PiP mode. There are\ndifferent requirements for apps running in either of these modes.\n\nThe default behavior is that the PiP app overlays the full-screen app. This is\nmuch the same as standard [Android picture-in-picture](/guide/topics/ui/picture-in-picture) behavior.\n\nNote that when integrating multitasking, your application must declare its\n[usage types](#usage-types) in\n[accordance with the TV app quality guidelines](/docs/quality-guidelines/tv-app-quality#TV-IC).\n\nRun your app in PiP mode\n------------------------\n\nFor TV devices running Android 14 (API level 34) or higher, run your app in PiP\nmode by calling [`enterPictureInPictureMode()`](/reference/android/app/Activity#enterPictureInPictureMode(android.app.PictureInPictureParams)). TV devices running earlier\nversions of Android don't support PiP mode.\n| **Note:** There is a deprecated `enterPictureInPictureMode()` method for API level 26 and earlier that does not use `PictureInPictureParams.Builder`. Avoid using it, because it's not supported on TV devices.\n\nHere is an example of how to implement the logic of a button to enter\nPiP mode: \n\n### Kotlin\n\n```kotlin\noverride fun onViewCreated(view: View, savedInstanceState: Bundle?) {\n super.onViewCreated(view, savedInstanceState)\n pictureInPictureButton.visibility =\n if (requireActivity().packageManager.hasSystemFeature(FEATURE_PICTURE_IN_PICTURE)) {\n pictureInPictureButton.setOnClickListener {\n val aspectRatio = Rational(view.width, view.height)\n val params = PictureInPictureParams.Builder()\n .setAspectRatio(aspectRatio)\n .build()\n val result = requireActivity().enterPictureInPictureMode(params)\n }\n View.VISIBLE\n } else {\n View.GONE\n }\n}\n```\n\n### Java\n\n```java\n@Override\npublic void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {\n super.onViewCreated(view, savedInstanceState);\n if (requireActivity().getPackageManager().hasSystemFeature(FEATURE_PICTURE_IN_PICTURE)) {\n pictureInPictureButton.setVisibility(View.VISIBLE);\n pictureInPictureButton.setOnClickListener(v -\u003e {\n Rational aspectRatio = new Rational(view.getWidth(), view.getHeight());\n PictureInPictureParams params = new PictureInPictureParams.Builder()\n .setAspectRatio(aspectRatio)\n .setTitle(\"My Streaming App\")\n .setSubtitle(\"My On-Demand Content\")\n .build();\n Boolean result = requireActivity().enterPictureInPictureMode(params);\n });\n } else {\n pictureInPictureButton.setVisibility(View.GONE);\n }\n}\n```\n\nThe action is only added if the device has the system feature\n[`FEATURE_PICTURE_IN_PICTURE`](/reference/kotlin/android/content/pm/PackageManager#feature_picture_in_picture). Also, when the action is triggered, the\naspect ratio of PiP mode is set to match the aspect ratio of the video being\nplayed.\n\nBe sure to add a [title](/reference/android/app/PictureInPictureParams.Builder#setTitle(java.lang.CharSequence)) and [subtitle](/reference/android/app/PictureInPictureParams.Builder#setSubtitle(java.lang.CharSequence)) to give the user information\nabout what this PIP is generally being used for.\n| **Note:** Entering picture-in picture mode requires explicit and intentional action by the user within the app. For details, see the [TV app quality guidelines](/docs/quality-guidelines/tv-app-quality#pip).\n\nCoexist with apps running in PiP mode\n-------------------------------------\n\nWhen your app is running as a fullscreen app it may need to adapt for other\napps running in PiP mode.\n\n### Keep-clear APIs\n\nIn some cases, the PiP app may overlay important UI components within the\nfullscreen app. To mitigate this, there are keep-clear APIs that apps can\nuse to identify critical UI components that shouldn't be overlaid. The system\nattempts to honor the requests to avoid covering these components by\nrepositioning the PiP window.\n\n| **Note:** The system might not honor these requests if too many UI components are marked as keep-clear, or if they take up large portions of the screen. Be mindful of this when using these APIs.\n\nTo specify that a view shouldn't be overlaid, use [`preferKeepClear`](/reference/android/R.attr#preferKeepClear) in your\nXML layout as in the following example: \n\n \u003cTextView\n android:id=\"@+id/important_text\"\n android:layout_width=\"wrap_content\"\n android:layout_height=\"wrap_content\"\n android:preferKeepClear=\"true\"\n android:text=\"@string/app_name\"/\u003e\n\nYou can also do this programmatically using [`setPreferKeepClear()`](/reference/android/view/View#setPreferKeepClear(boolean)): \n\n### Kotlin\n\n```kotlin\nprivate lateinit var binding: MyLayoutBinding\n\noverride fun onCreate(savedInstanceState: Bundle?) {\n super.onCreate(savedInstanceState)\n\n binding = MyLayoutBinding.inflate(layoutInflater)\n setContentView(binding.root)\n binding.importantText.isPreferKeepClear = true\n}\n```\n\n### Java\n\n```java\nprivate MyLayoutBinding binding;\n\n@Override\nprotected void onCreate(@Nullable Bundle savedInstanceState) {\n super.onCreate(savedInstanceState);\n\n binding = MyLayoutBinding.inflate(getLayoutInflater());\n setContentView(binding.getRoot());\n binding.importantText.setPreferKeepClear(true);\n}\n```\n\nThere may be times when you don't need to keep an entire [`View`](/reference/android/view/View) clear, but\nonly a section of it. The [`setPreferKeepClearRects()`](/reference/android/view/View#setPreferKeepClearRects(java.util.List%3Candroid.graphics.Rect%3E)) can be used to\nspecify regions of the `View` that shouldn't be overlaid. UIs that don't use\n`View`s natively, such as Flutter, Jetpack Compose, and WebView, may have\nsub-sections that need regions kept clear. This API can be used for those cases.\n| **Note:** If you set [`setPreferKeepClear()`](/reference/android/view/View#setPreferKeepClear(boolean)) to `true`, the entire `View` is marked as keep clear, and any regions specified using [`setPreferKeepClearRects()`](/reference/android/view/View#setPreferKeepClearRects(java.util.List%3Candroid.graphics.Rect%3E)) are ignored.\n\nUsage types\n-----------\n\nYour app must declare a [meta-data value attribute](/guide/topics/manifest/meta-data-element) of\n`com.google.android.tv.pip.category` that corresponds with the primary type or\ntypes of usage for the picture-in-picture mode. Any `\u003cactivity\u003e` that has set\n`android:supportsPictureInPicture=\"true\"` should declare this attribute with a\nrelevant value from the table below.\n\nUsage types that don't fall into any of these categories, in particular any\nplayback of media content, are not allowed in picture-in-picture mode on TV.\n\n| Value | Description |\n|-------------------|-------------------------------------------------------------------------|\n| \"`communication`\" | Communications use cases, such as video or voice calls. |\n| \"`smartHome`\" | Smart home integrations, such as connected doorbells or baby monitors. |\n| \"`health`\" | Health use cases, such as fitness tracking or health monitoring. |\n| \"`ticker`\" | Ticker use cases, such as live sports scores or news and stock tickers. |\n\nMultiple values are separated by a vertical bar (`|`). For example: \n\n```xml\n\u003cmeta-data android:name=\"com.google.android.tv.pip.category\" android:value=\"smartHome|health\" /\u003e\n```\n\n\u003cbr /\u003e"]]