你可以添加Chromecast的功能,而无需使用动作条 [英] Can you add Chromecast functionality without using the ActionBar

查看:145
本文介绍了你可以添加Chromecast的功能,而无需使用动作条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从示例应用程序从谷歌到$ P $工作ppare我们的Chromecast能力,

I've been working from the example applications from Google to prepare our Chromecast capabilities,

什么我却发现是你的需要有一个按钮动作条(由谷歌来实现),以获得 ActionProvider ,以便附上你的选择器。

What I've found however is that you are required to have a button in the ActionBar (as implemented by Google) in order to get an ActionProvider in order to attach your selector to.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.main, menu);
    MenuItem mediaRouteMenuItem = menu.findItem(R.id.media_route_menu_item);
    MediaRouteActionProvider mediaRouteActionProvider = (MediaRouteActionProvider) MenuItemCompat.getActionProvider(mediaRouteMenuItem);
    mediaRouteActionProvider.setRouteSelector(mCaster.getMediaRouteSelector());
    return true;
}

完好,整个例子和API似乎是热连接到围绕着利用该按钮的。我很好再现的外观和感觉的机器人试图去与这里 - 但我在我的应用程序,这意味着我没有任何挂钩,选择路线等人使用自定义动作条。

Intact, the entire example and API seems to be hot-wired to centre around the use of this button. I'm fine with reproducing the look and feel the Android are trying to go with here - but I use a custom ActionBar in my app which means I don't have any "hooks" to select the route et al.

有谁知道如何解决此问题?

Does anyone know how to work around this?

推荐答案

首先,我建议你考虑迁移到appcompat动作条,所以你可以得到所有的好处前进开箱。

To begin with, I suggest you consider moving to appcompat actionbar so you can get all the benefits moving forward out of the box.

这就是说,回答你的问题是:是的,这是可行的,如果你愿意做一些额外的工作来管理生命周期,并指出自己。我概述的步骤在这里,所以你可以创建一个工作示例为自己。该要求是要有appcompat和mediarouter从V7支持库中的项目,但你并不需要使用动作条或根本没有必要使用MediaRouteButton任何具体类型;其实你可以触发发现,其余在任何形状或形式,你想要的。具体操作步骤如下:

That said, the answer to your question is: yes, it is doable if you are willing to do a bit of extra work to manage the lifecycle and states yourself. I outline the steps here so you can create a working example for yourself. The requirements is to have appcompat and mediarouter from v7 support library in your project but you don't need to use any specific type of actionbar or no need to use the MediaRouteButton; in fact you can trigger the discovery and the rest in any shape or form that you want. Here are the steps:

  • 确认appcompat和mediarouter从V7支持库都包含在你的依赖。有mediarouter的V7版本是非常重要的。

  • Make sure appcompat and mediarouter from v7 support library are included in your dependencies. It is important to have v7 version of mediarouter

做平常的事情,即建立一个 CastContext ,并搭建了舞台准备发现(你需要创建 MediaRouteAdapter的一个实例,我已经叫 myMediaRouteAdapter ):

Do the usual thing, i.e. build a CastContext and set the stage ready for discovery (you need to create an instance of MediaRouteAdapter, which I have called myMediaRouteAdapter):


 mCastContext = new CastContext(getApplicationContext());
 MediaRouteHelper.registerMinimalMediaRouteProvider(mCastContext, 
       myMediaRouteAdapter);
 mMediaRouter = MediaRouter.getInstance(getApplicationContext());
 mMediaRouteSelector = MediaRouteHelper
    .buildMediaRouteSelector(MediaRouteHelper.CATEGORY_CAST,
                                        "YOUR_APP_NAME", null);
 mMediaRouterCallback = new MyMediaRouterCallback();

  • 通过调用启动发现你的,比方说,ONSTART()以下内容:
  • 
     mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback,
                            MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY);
    

    • 在自定义的回调 MyMediaRouterCallback ,监听,因为他们得到添加或删除路线。由于路线得到发现,你的回调的 onRouteAdded()方法将被调用,当他们离开时, onRouteRemoved()将被调用。这将是现在你的责任,以保住在您所选择的数据结构的有效路由列表。

      • In your custom callback MyMediaRouterCallback, listen for routes as they get added or removed. As routes get discovered, your onRouteAdded() method of your callback will be called and as they go away, the onRouteRemoved() will be called. It will be now your responsibility to hold on to the list of valid routes in your choice of data structure.

        可以说,为了便于讨论,你present列表,用户在对话框(有道理的,不是吗?),其中用户认为,在路线(即设备)的名称用户界面,并重新$ P $每个项目psents一个 RouteInfo 。然后,当用户点击一个路线,你需要调用

        Lets say, for the sake of argument, you present the list to users in a dialog (makes sense, doesn't it?) where user sees a the name of route (i.e. device) in the UI and each item represents a RouteInfo. Then when a user clicks on a route, you need to call

        
        mMediaRouter.selectRoute(info);
        
        

        • 在自定义的回调的另一个回调以上调用结果被调用时, onRouteSelected()。在该回调,请拨打以下内容:
          • The above call results in another callback of your custom callback to be called, the onRouteSelected(). In that callback, call the following:
          • 
            MediaRouteHelper.requestCastDeviceForRoute(info);
            
            

            • 在最后,这将调用 myMediaRouteAdapter onDeviceAvailable()回调并传递 CastDevice 给你,你应该能够抓住并用。当你完成了一个途径,并希望取消了,叫
              • Finally, this will call the onDeviceAvailable() callback of the myMediaRouteAdapter and passes a CastDevice to you which you should be able to grab and use. When you are done with a route and want to "deselect" it, call
              • 
                mMediaRouter.selectRoute(mMediaRouter.getDefaultRoute());
                
                

                希望这是足以让你去。

                Hopefully that is enough to get you going.

                这篇关于你可以添加Chromecast的功能,而无需使用动作条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆