[英]How can I initialize my RelativeLayoutButton outside of onCreate method?
[英]How to initialize CastContext outside of onCreate method
我想只根據某些標准激活Cast功能,所以我不想在onCreate中使用任何Cast邏輯。 我有一個setupCast方法,它具有以下代碼:
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時,我的Cast按鈕不會顯示。
但是,如果我只移動:
mCastContext = CastContext.getSharedInstance(_movieActivityContext);
進入我的onCreate方法並保持其他所有方式,Cast按鈕顯示。
您可以使用onStart (android.content.Intent,int,int)。 每次客戶端通過調用startService(Intent)顯式啟動服務時,由系統調用,提供它提供的參數和表示啟動請求的唯一整數標記。 不要直接調用此方法。
// 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 。
要正確初始化CastContext ,應用程序必須具有實現OptionsProvider接口的類:
package com.example.app;
public class CastOptionsProvider implements OptionsProvider {
@Override
public CastOptions getCastOptions(Context appContext) {
...
}
}
並在AndroidManifest.xml中使用鍵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" />
...
必須從主線程調用所有公共方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.