Android Chrome浏览器无法获得回叫,如果Android设备位于同一个wifi网络中,并且不在Chrome浏览器范围内

时间:2018-10-01 12:44:15

标签: android google-chrome

我正在尝试管理投射按钮的可见性,当它处于chrome-cast范围内时(使可见性消失/根据范围和wifi n / w消失)。

回调并不是一直都在被调用。每当从后台杀死我的应用程序并重新启动它的调用时,否则都不会被调用。

下面是我的代码。

public  class MainActivity extends AppCompatActivity{

private MediaRouter.Callback mMediaRouterCallback;
private MediaRouter mMediaRouter;
private MediaRouteSelector mMediaRouteSelector;
private MediaRouteButton mMediaRouteButton;
private int mRouteCount = 0;
private CastDevice mSelectedDevice;

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
    initChromeCast();

    }

     @Override
        protected void onStart() {
            super.onStart();
            mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback,
                    MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY);
        }

       /**
         * init chrome cast
         *
         * @param view
         */
        protected void initChromeCast() {          
                                CastButtonFactory.setUpMediaRouteButton(mContext.getApplicationContext(), (MediaRouteButton) view.findViewById(R.id.media_route_button));
                int playServicesAvailable =
                        GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(mContext);
                        if (playServicesAvailable == ConnectionResult.SUCCESS) {
                }

                mMediaRouterCallback = new MediaRouterCallback();
                mMediaRouter = MediaRouter.getInstance(mContext.getApplicationContext());
                mMediaRouteSelector = new MediaRouteSelector.Builder()

                        .addControlCategory(                                  CastMediaControlIntent.categoryForCast(CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID)).build();       
                        mMediaRouteButton = (MediaRouteButton)findViewById(R.id.media_route_button);
                mMediaRouteButton.setRouteSelector(mMediaRouteSelector);                  

            }
        }        

     private class MediaRouterCallback extends MediaRouter.Callback {
            @Override
            public void onRouteAdded(MediaRouter router, MediaRouter.RouteInfo route) {
                Log.d(TAG, "----onRouteAdded");
                if (++mRouteCount == 1) {
                    mMediaRouteButton.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onRouteRemoved(MediaRouter router, MediaRouter.RouteInfo route) {
                Log.d(TAG, "----onRouteRemoved");
                if (--mRouteCount == 0) {
                    // Hide the button if there are no devices discovered.
                    mMediaRouteButton.setVisibility(View.GONE);
                    mMediaRouteButton.setDrawingCacheBackgroundColor(ContextCompat.getColor(mContext,R.color.fb_button_color));
                }
            }

            @Override
            public void onRouteSelected(MediaRouter router, MediaRouter.RouteInfo info) {
                Log.d(TAG, "----onRouteSelected");
                mSelectedDevice = CastDevice.getFromBundle(info.getExtras());

                // Just display a message for now; In a real app this would be the
                // hook to connect to the selected device and launch the receiver
                // app
            }

            @Override
            public void onRouteUnselected(MediaRouter router, MediaRouter.RouteInfo info) {
                Log.d(TAG, "----onRouteUnselected: info=" + info);
                mSelectedDevice = null;
            }
        }


}

0 个答案:

没有答案