8

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
    });
nhcodes
  • 1,206
  • 8
  • 20

3 Answers3

0

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
Jonathan ANTOINE
  • 9,021
  • 1
  • 23
  • 33
0

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());
Fakrudeen
  • 5,778
  • 7
  • 44
  • 70
0

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
Praveen
  • 3,186
  • 2
  • 8
  • 23