public final class

MediaTransferReceiver

extends BroadcastReceiver

 java.lang.Object

↳BroadcastReceiver

↳androidx.mediarouter.media.MediaTransferReceiver

Gradle dependencies

compile group: 'androidx.mediarouter', name: 'mediarouter', version: '1.7.0'

  • groupId: androidx.mediarouter
  • artifactId: mediarouter
  • version: 1.7.0

Artifact androidx.mediarouter:mediarouter:1.7.0 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.mediarouter:mediarouter com.android.support:mediarouter-v7

Overview

A BroadcastReceiver class for enabling Media transfer feature.

Media transfer is a feature that media routing can be controlled via system UI. By using this, media app users can re-route the media without opening the app activity again. Also, the media can be transferred from one device to another device seamlessly, depending on the devices. This feature is supported from Android 11.

To enable the media transfer feature, media apps should declare this receiver in the app's manifest. For example:


     
 
 

Media apps that enable this feature should implement the MediaRouter.Callback properly. Specifically:

  • Apps should be able to get events even when the app is in background. This means that the callback should not be removed in . (See MediaRouter.addCallback(MediaRouteSelector, MediaRouter.Callback, int) for how to add callback.
  • Apps should handle the case where the media routing is changed from the outside of the app. The callback's onRouteSelected method should be able to handle the cases.
  • In order to enable transferring media from remote to local (e.g. from TV to phone), media apps should enable 'transfer to local' feature. Otherwise, the local devices won't be shown as a transfer target while playing on a remote device.

Summary

Constructors
publicMediaTransferReceiver()

Methods
public static booleanisDeclared(Context applicationContext)

Check whether the MediaTransferReceiver is declared in the app manifest.

public voidonReceive(Context context, Intent intent)

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public MediaTransferReceiver()

Methods

public void onReceive(Context context, Intent intent)

public static boolean isDeclared(Context applicationContext)

Check whether the MediaTransferReceiver is declared in the app manifest.

Source

/*
 * Copyright 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.mediarouter.media;

import static androidx.annotation.RestrictTo.Scope.LIBRARY;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;

import androidx.annotation.NonNull;
import androidx.annotation.RestrictTo;

import java.util.List;

/**
 * A {@link BroadcastReceiver} class for enabling Media transfer feature.
 * <p>
 * Media transfer is a feature that media routing can be controlled via system UI. By using this,
 * media app users can re-route the media without opening the app activity again. Also, the media
 * can be transferred from one device to another device seamlessly, depending on the devices.
 * This feature is supported from Android 11.
 * <p>
 * To enable the media transfer feature, media apps should declare this receiver in the app's
 * manifest. For example:
 * <pre class="prettyprint">{@code
 * <application>
 *     <receiver android:name="androidx.mediarouter.media.MediaTransferReceiver" />
 * </application>
 * }</pre>
 * <p>
 * Media apps that enable this feature should implement the {@link MediaRouter.Callback} properly.
 * Specifically:
 * <ul>
 *     <li>Apps should be able to get events even when the app is in background. This means
 *         that the callback should not be removed in {@link Activity#onStop()}. (See
 *         {@link MediaRouter#addCallback(MediaRouteSelector, MediaRouter.Callback, int)} for
 *         how to add callback.</li>
 *     <li>Apps should handle the case where the media routing is changed from the outside of the
 *         app. The callback's
 *         {@link MediaRouter.Callback#onRouteSelected(MediaRouter, MediaRouter.RouteInfo, int)
 *         onRouteSelected} method should be able to handle the cases.</li>
 *     <li>In order to enable transferring media from remote to local (e.g. from TV to phone),
 *         media apps should {@link MediaRouterParams.Builder#setTransferToLocalEnabled(boolean)
 *         enable 'transfer to local' feature}. Otherwise, the local devices won't be shown as a
 *         transfer target while playing on a remote device.
 * </ul>
 */
// TODO: Mention that devs should implement onPrepareTransfer() - after the API is ready.
public final class MediaTransferReceiver extends BroadcastReceiver  {
    @Override
    public void onReceive(@NonNull Context context, @NonNull Intent intent) {
        // Do nothing for now.
    }

    /**
     * Check whether the {@link MediaTransferReceiver} is declared in the app manifest.
     */
    @RestrictTo(LIBRARY)
    public static boolean isDeclared(@NonNull Context applicationContext) {
        Intent queryIntent = new Intent(applicationContext, MediaTransferReceiver.class);
        queryIntent.setPackage(applicationContext.getPackageName());
        PackageManager pm = applicationContext.getPackageManager();
        List<ResolveInfo> resolveInfos = pm.queryBroadcastReceivers(queryIntent, 0);

        return resolveInfos.size() > 0;
    }
}