问题
I don't want to use google cast code to connect devices from my app but needed to just check whether the phone is connected to any cast Device.
Do we get any Receiver/listener for this check?
Thanks!
EDIT: I have tried to get Cast State as following as mentioned by Chris,
- Add following in Manifest
<meta-data android:name="com.google.android.gms.cast.framework.OPTIONS_PROVIDER_CLASS_NAME" android:value=".CastOptionsProvider" />
- Create CastOptionProvider Class
public class CastOptionsProvider implements OptionsProvider { @Override public CastOptions getCastOptions(Context context) { return new CastOptions.Builder() .build(); } @Override public List<SessionProvider> getAdditionalSessionProviders(Context context) { return null; } }
- Reading state in MainActivity as,
CastContext mCastContext = CastContext.getSharedInstance(context); if (mCastContext != null) { int x = mCastContext.getCastState();
}
Here Value of x is always "1" i.e. no Devices Available, eventhough the phone is connected to any Miracast Device.
回答1:
CastContext:
https://developers.google.com/android/reference/com/google/android/gms/cast/framework/CastContext
getCastState will tell you whether you're connected or you can call addCastStateListener to detect when the state changes.
来源:https://stackoverflow.com/questions/51683422/broadcast-receiver-for-cast-connectivity-in-android