如何从 android-tv 上的主屏幕节目预览启动特定的 LiveChannels 频道

How to launch specific LiveChannels channel from home screen programpreview on android-tv

如果您完全熟悉 tv-input-framework 和 livechannels 以及家庭频道节目预览,那么我有一个问题要问您。我正在尝试创建一个主屏幕频道,它将通过我们的电视输入服务列出最近观看的直播频道。我可以很好地列出它们,但将预览程序启动到指定的 "live channel" 似乎是不可能的。有人知道怎么做吗?

    Intent intent = context.getPackageManager().getLaunchIntentForPackage("com.google.android.tv");
    intent.setAction(Intent.ACTION_VIEW);
    intent.setData(TvContract.buildChannelUri(channel.getId()));

在深入了解 LiveChannel 的来源后,我找到了答案

static long addPreviewProgram(Context context, long homeChannelId, Uri liveChannelUri){
    PreviewProgram.Builder builder = new PreviewProgram.Builder();

    Intent intent = new Intent();
    intent.setComponent(new ComponentName("com.google.android.tv", "com.android.tv.MainActivity"));
    intent.setAction(Intent.ACTION_VIEW);
    intent.setData(liveChannelUri);

    builder.setChannelId(homeChannelId)
            .setType(PreviewPrograms.TYPE_CLIP)
            .setIntent(intent);

    Uri programUri = context.getContentResolver().insert(PreviewPrograms.CONTENT_URI, builder.build().toContentValues());

    return ContentUris.parseId(programUri);

}

意图操作和通道集发生在 LiveChannel 的 MainActivity 中,但 MainActivity 不是启动器 activity 因此您必须将其指定为要启动的 activity。必须将操作设置为 Intent.ACTION_VIEW,否则永远不会设置频道。