CastContext.getSharedInstance(Context) is now deprecated

2k views Asked by At

As the title says, CastContext.getSharedInstance(Context) is now deprecated:

getSharedInstance(Context context): This method is deprecated. Use getSharedInstance(Context, Executor) instead to handle the exception when Cast SDK fails to load the internal Cast module. https://developers.google.com/android/reference/com/google/android/gms/cast/framework/CastContext

What would be the correct way to specify an Executor and return the CastContext? I got it working like this but I wonder if this is the best way to do it:

CastContext
    .getSharedInstance(context, Executors.newSingleThreadExecutor())
    .addOnSuccessListener(castContext -> {
        //do something with castContext
    })
    .addOnFailureListener(exception -> {
        //throw exception
    });
3

There are 3 answers

0
Jonathan ANTOINE On

This is exactly the correct way to use it. In Xamarin (C#) you would do it this way :

var instanceTask =  CastContext.GetSharedInstance(Application.Context, Executors.NewSingleThreadExecutor());
var sharedInstance = await instanceTask as CastContext;

// do something with it
0
Fakrudeen On

We could simply use the executor service already used for other background processing instead of creating a brand new one:

CastContext.getSharedInstance(getApplicationContext(), Common.getBackgroundProcessor());
1
Praveen On

Your approach is good for retrieving CastContext asynchronously.

But, in the official cast sample app, they are retrieving CastContext synchronously by directly calling getResult() on the getSharedInstance() task.

val castExecutor = Executors.newSingleThreadExecutor()
val castContext = CastContext.getSharedInstance(this, castExecutor).result