如何在onCreate方法之外初始化CastContext [英] How to initialize CastContext outside of onCreate method

查看:291
本文介绍了如何在onCreate方法之外初始化CastContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些条件,我只想激活Cast功能,所以我不想在onCreate中使用任何Cast逻辑。我有一个具有以下代码的setupCast方法:

I would like to only activate the Cast feature due to certain criteria so I don't want any Cast logic in my onCreate. I have a setupCast method that has the following code:

private void setupCast(String appId) {
    if (appId != null) {
        Log.d(TAG, "Setting up Cast...");
        setupCastListener();
        CastOptionsProvider.setAppId(appId);
        mCastContext = CastContext.getSharedInstance(_movieActivityContext);
        mCastContext.registerLifecycleCallbacksBeforeIceCreamSandwich(this,      _savedInstanceState);
        mCastSession = mCastContext.getSessionManager().getCurrentCastSession();
        mCastContext.getSessionManager().addSessionManagerListener(
                mSessionManagerListener, CastSession.class);
        mediaRouteMenuItem = CastButtonFactory.setUpMediaRouteButton(getApplicationContext(), _menuForChromecastButton, R.id.media_route_menu_item);
    }
}

应用程序接收者ID通过http调用传递并且如果appId为null,则不要启动Cast。我遇到的问题是,以这种方式初始化mCastContext时,我的投射按钮没有显示。

The application receiver id is passed in through an http call and if the appId is null, don't start Cast. The problem I'm having is that my Cast button does not show up when initializing mCastContext this way.

但是,如果我只移动:

mCastContext = CastContext.getSharedInstance(_movieActivityContext );

进入我的onCreate方法并保持其他所有方式不变,将显示投射按钮。

Into my onCreate method and keep everything else the way it is, the Cast button shows up.

推荐答案

您可以使用 onStart (android.content.Intent,int,int)。每当客户端通过调用startService(Intent)显式启动服务时,系统都会调用此方法,并提供其提供的参数和代表启动请求的唯一整数令牌。不要直接调用此方法。

You can use onStart(android.content.Intent, int, int). Called by the system every time a client explicitly starts the service by calling startService(Intent), providing the arguments it supplied and a unique integer token representing the start request. Do not call this method directly.

// This is the old onStart method that will be called on the pre-2.0
// platform. On 2.0 or later we override onStartCommand() so this
// method will not be called.
@Override
public void onStart(Intent intent, int startId) {
handleCommand(intent);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
handleCommand(intent);
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}

请注意,系统会在您服务的主线程上调用它。服务的主线程与在同一进程中运行的活动进行UI操作的线程相同。您应始终避免使主线程的事件循环停顿。在进行长时间运行的操作,网络调用或大容量磁盘I / O时,您应该启动新线程或使用 AsyncTask

Note that the system calls this on your service's main thread. A service's main thread is the same thread where UI operations take place for Activities running in the same process. You should always avoid stalling the main thread's event loop. When doing long-running operations, network calls or heavy disk I/O, you should kick off a new thread or use AsyncTask.

正确初始化 CastContext ,应用程序必须具有实现OptionsProvider接口的类:

To properly initialize CastContext, the application must have a class that implements OptionsProvider interface:

package com.example.app;

public class CastOptionsProvider implements OptionsProvider {
@Override
public CastOptions getCastOptions(Context appContext) {
...
}
}

并在AndroidManifest.xml中使用键 OPTIONS_PROVIDER_CLASS_NAME_KEY

and specify its fully qualified class name in the AndroidManifest.xml with the key OPTIONS_PROVIDER_CLASS_NAME_KEY

..

...
<meta-data
android:name="com.google.android.gms.cast.framework.OPTIONS_PROVIDER_CLASS_NAME"
android:value="com.example.app.CastOptionsProvider" />
...

所有公共方法都必须从主线程调用。

All public methods must be called from the main thread.

这篇关于如何在onCreate方法之外初始化CastContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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