Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Methods](https://developer.android.com/reference/android/media/projection/MediaProjectionManager#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/media/projection/MediaProjectionManager#inhmethods)

# MediaProjectionManager

*** ** * ** ***


`
public

final

class
MediaProjectionManager
`


`

extends https://developer.android.com/reference/java/lang/Object


`

`


`

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.media.projection.MediaProjectionManager |

<br />

*** ** * ** ***

Manages the retrieval of certain types of `https://developer.android.com/reference/android/media/projection/MediaProjection` tokens.


An example flow of starting a media projection will be:
1. Declare a foreground service with the type `mediaProjection` in the `AndroidManifest.xml`.
2. Create an intent by calling `https://developer.android.com/reference/android/media/projection/MediaProjectionManager#createScreenCaptureIntent()` and pass this intent to `https://developer.android.com/reference/android/app/Activity#startActivityForResult(android.content.Intent,%20int)`.
3. On getting `https://developer.android.com/reference/android/app/Activity#onActivityResult(int,%20int,%20android.content.Intent)`, start the foreground service with the type `https://developer.android.com/reference/android/content/pm/ServiceInfo#FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION`.
4. Retrieve the media projection token by calling `https://developer.android.com/reference/android/media/projection/MediaProjectionManager#getMediaProjection(int,%20android.content.Intent)` with the result code and intent from the `https://developer.android.com/reference/android/app/Activity#onActivityResult(int,%20int,%20android.content.Intent)` above.
5. Register a `https://developer.android.com/reference/android/media/projection/MediaProjection.Callback` by calling `https://developer.android.com/reference/android/media/projection/MediaProjection#registerCallback(android.media.projection.MediaProjection.Callback,%20android.os.Handler)`. This is required to receive notifications about when the `https://developer.android.com/reference/android/media/projection/MediaProjection` or captured content changes state. When receiving an \`onStop()\` callback the `https://developer.android.com/reference/android/media/projection/MediaProjection` session has been finished and the client must clean up any resources it is holding, e.g. the `https://developer.android.com/reference/android/hardware/display/VirtualDisplay` and `https://developer.android.com/reference/android/view/Surface`. The MediaProjection may further no longer create any new `https://developer.android.com/reference/android/hardware/display/VirtualDisplay`s via `https://developer.android.com/reference/android/media/projection/MediaProjection#createVirtualDisplay(java.lang.String,%20int,%20int,%20int,%20int,%20android.view.Surface,%20android.hardware.display.VirtualDisplay.Callback,%20android.os.Handler)`. Note that the \`onStop()\` callback can be a result of the system stopping MediaProjection due to various reasons. This includes the user stopping the MediaProjection via UI affordances in system-level UI, the screen being locked, or another `https://developer.android.com/reference/android/media/projection/MediaProjection` session starting.
6. Start the screen capture session for media projection by calling `https://developer.android.com/reference/android/media/projection/MediaProjection#createVirtualDisplay(java.lang.String,%20int,%20int,%20int,%20int,%20android.view.Surface,%20android.hardware.display.VirtualDisplay.Callback,%20android.os.Handler)`.

<br />

## Summary

| ### Public methods ||
|---|---|
| ` https://developer.android.com/reference/android/content/Intent` | ` https://developer.android.com/reference/android/media/projection/MediaProjectionManager#createScreenCaptureIntent()() ` Returns an `https://developer.android.com/reference/android/content/Intent` that **must** be passed to `https://developer.android.com/reference/android/app/Activity#startActivityForResult(android.content.Intent,%20int)` (or similar) in order to start screen capture. |
| ` https://developer.android.com/reference/android/content/Intent` | ` https://developer.android.com/reference/android/media/projection/MediaProjectionManager#createScreenCaptureIntent(android.media.projection.MediaProjectionConfig)(https://developer.android.com/reference/android/media/projection/MediaProjectionConfig config) ` Returns an `https://developer.android.com/reference/android/content/Intent` that **must** be passed to `https://developer.android.com/reference/android/app/Activity#startActivityForResult(android.content.Intent,%20int)` (or similar) in order to start screen capture. |
| ` https://developer.android.com/reference/android/media/projection/MediaProjection` | ` https://developer.android.com/reference/android/media/projection/MediaProjectionManager#getMediaProjection(int,%20android.content.Intent)(int resultCode, https://developer.android.com/reference/android/content/Intent resultData) ` Retrieves the `https://developer.android.com/reference/android/media/projection/MediaProjection` obtained from a successful screen capture request. |

| ### Inherited methods |
|---|---|
| From class ` https://developer.android.com/reference/java/lang/Object ` |---|---| | ` https://developer.android.com/reference/java/lang/Object` | ` https://developer.android.com/reference/java/lang/Object#clone()() ` Creates and returns a copy of this object. | | ` boolean` | ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) ` Indicates whether some other object is "equal to" this one. | | ` void` | ` https://developer.android.com/reference/java/lang/Object#finalize()() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. | | ` final https://developer.android.com/reference/java/lang/Class<?>` | ` https://developer.android.com/reference/java/lang/Object#getClass()() ` Returns the runtime class of this `Object`. | | ` int` | ` https://developer.android.com/reference/java/lang/Object#hashCode()() ` Returns a hash code value for the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notify()() ` Wakes up a single thread that is waiting on this object's monitor. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notifyAll()() ` Wakes up all threads that are waiting on this object's monitor. | | ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/lang/Object#toString()() ` Returns a string representation of the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait()() ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*. | ||

## Public methods

### createScreenCaptureIntent

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public Intent createScreenCaptureIntent ()
```

Returns an `https://developer.android.com/reference/android/content/Intent` that **must** be passed to
`https://developer.android.com/reference/android/app/Activity#startActivityForResult(android.content.Intent,%20int)` (or similar) in order to start screen
capture. The activity will prompt the user whether to allow screen capture. The result of
this activity (received by overriding `https://developer.android.com/reference/android/app/Activity#onActivityResult(int,%20int,%20android.content.Intent)`) should be passed to
`https://developer.android.com/reference/android/media/projection/MediaProjectionManager#getMediaProjection(int,%20android.content.Intent)`.


Identical to calling `https://developer.android.com/reference/android/media/projection/MediaProjectionManager#createScreenCaptureIntent(android.media.projection.MediaProjectionConfig)` with
a `https://developer.android.com/reference/android/media/projection/MediaProjectionConfig#createConfigForUserChoice()`.


Should be used instead of `https://developer.android.com/reference/android/media/projection/MediaProjectionManager#createScreenCaptureIntent(android.media.projection.MediaProjectionConfig)` when the
calling app does not want to customize the activity shown to the user.

<br />

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/content/Intent` | This value cannot be `null`. <br /> |

### createScreenCaptureIntent

Added in [API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public Intent createScreenCaptureIntent (MediaProjectionConfig config)
```

Returns an `https://developer.android.com/reference/android/content/Intent` that **must** be passed to
`https://developer.android.com/reference/android/app/Activity#startActivityForResult(android.content.Intent,%20int)` (or similar) in order to start screen
capture. Customizes the activity and resulting `https://developer.android.com/reference/android/media/projection/MediaProjection` session based up
the provided `config`. The activity will prompt the user whether to allow screen
capture. The result of this activity (received by overriding
`https://developer.android.com/reference/android/app/Activity#onActivityResult(int,%20int,%20android.content.Intent)`)
should be passed to `https://developer.android.com/reference/android/media/projection/MediaProjectionManager#getMediaProjection(int,%20android.content.Intent)`.


If `https://developer.android.com/reference/android/media/projection/MediaProjectionConfig` was created from:

- `https://developer.android.com/reference/android/media/projection/MediaProjectionConfig#createConfigForDefaultDisplay()`, then creates an `https://developer.android.com/reference/android/content/Intent` for capturing the default display. The activity limits the user's choice to just the display specified.
- `https://developer.android.com/reference/android/media/projection/MediaProjectionConfig#createConfigForUserChoice()`, then creates an `https://developer.android.com/reference/android/content/Intent` for deferring which region to capture to the user. This gives the user the same behaviour as calling `https://developer.android.com/reference/android/media/projection/MediaProjectionManager#createScreenCaptureIntent()`. The activity gives the user the choice between `https://developer.android.com/reference/android/view/Display#DEFAULT_DISPLAY`, or a different region.

<br />


Should be used instead of `https://developer.android.com/reference/android/media/projection/MediaProjectionManager#createScreenCaptureIntent()` when the calling app wants to
customize the activity shown to the user.

<br />

<br />

| Parameters ||
|---|---|
| `config` | `MediaProjectionConfig`: Customization for the `https://developer.android.com/reference/android/media/projection/MediaProjection` that this `https://developer.android.com/reference/android/content/Intent` requests the user's consent for. This value cannot be `null`. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/content/Intent` | An `https://developer.android.com/reference/android/content/Intent` requesting the user's consent, specialized based upon the given configuration. This value cannot be `null`. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if `https://developer.android.com/reference/android/media/projection/MediaProjectionConfig#isOwnAppContentProvided()` is true but no `https://developer.android.com/reference/android/media/projection/AppContentProjectionService` is declared. |

### getMediaProjection

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public MediaProjection getMediaProjection (int resultCode, 
                Intent resultData)
```

Retrieves the `https://developer.android.com/reference/android/media/projection/MediaProjection` obtained from a successful screen
capture request. The result code and data from the request are provided by overriding
`https://developer.android.com/reference/android/app/Activity#onActivityResult(int,%20int,%20android.content.Intent)`,
which is called after starting an activity using `https://developer.android.com/reference/android/media/projection/MediaProjectionManager#createScreenCaptureIntent()`.


Starting from Android `https://developer.android.com/reference/android/os/Build.VERSION_CODES#R`, if your application
requests the `https://developer.android.com/reference/android/Manifest.permission#SYSTEM_ALERT_WINDOW`
permission, and the user has not explicitly denied it, the permission will be automatically
granted until the projection is stopped. The permission allows your app to display user
controls on top of the screen being captured.


An app targeting SDK version `https://developer.android.com/reference/android/os/Build.VERSION_CODES#Q` or later must
invoke `getMediaProjection` and maintain the capture session
(`https://developer.android.com/reference/android/media/projection/MediaProjection#createVirtualDisplay(java.lang.String,%20int,%20int,%20int,%20int,%20android.view.Surface,%20android.hardware.display.VirtualDisplay.Callback,%20android.os.Handler)`) while running a foreground service. The app must set
the `https://developer.android.com/reference/android/R.attr#foregroundServiceType` attribute to
`https://developer.android.com/reference/android/content/pm/ServiceInfo#FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION` in the
[`<service>`](https://developer.android.com/guide/topics/manifest/service-element) element of
the app's manifest file.


For an app targeting SDK version `https://developer.android.com/reference/android/os/Build.VERSION_CODES#UPSIDE_DOWN_CAKE` or
later, the user must have granted the app with the permission to start a projection,
before the app starts a foreground service with the type
`https://developer.android.com/reference/android/content/pm/ServiceInfo#FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION`.
Additionally, the app must have started the foreground service with that type before calling
this API here, or else it'll receive a `https://developer.android.com/reference/java/lang/SecurityException` from this API call, unless
it's a privileged app. Apps can request the permission via the
`https://developer.android.com/reference/android/media/projection/MediaProjectionManager#createScreenCaptureIntent()` and `https://developer.android.com/reference/android/app/Activity#startActivityForResult(android.content.Intent,%20int)`
(or similar APIs).

<br />

<br />

| Parameters ||
|---|---|
| `resultCode` | `int`: The result code from `https://developer.android.com/reference/android/app/Activity#onActivityResult(int,%20int,%20android.content.Intent)`. <br /> |
| `resultData` | `Intent`: The result data from `https://developer.android.com/reference/android/app/Activity#onActivityResult(int,%20int,%20android.content.Intent)`. This value cannot be `null`. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/media/projection/MediaProjection` | The media projection obtained from a successful screen capture request, or null if the result of the screen capture request is not `https://developer.android.com/reference/android/app/Activity#RESULT_OK`. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException` | On pre-`https://developer.android.com/reference/android/os/Build.VERSION_CODES#Q` devices if a previously obtained `MediaProjection` from the same `resultData` has not yet been stopped. |
| `https://developer.android.com/reference/java/lang/SecurityException` | On `https://developer.android.com/reference/android/os/Build.VERSION_CODES#Q`+ devices if not invoked from a foreground service with type `https://developer.android.com/reference/android/content/pm/ServiceInfo#FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION`, unless caller is a privileged app. |

**See also:**

- [Foreground services developer guide](https://developer.android.com/guide/components/foreground-services)
- [Media projection developer guide](https://developer.android.com/guide/topics/large-screens/media-projection)