程序未从Android TV的主屏幕启动

时间:2017-11-22 17:31:08

标签: android-tv

我正在尝试在主屏幕上选择一个程序但是我一直在举报“现在无法打开它”。

这是我的清单以及我的计划如何添加到电视提供商。我正在设置意图uri,内部提供者ID和内容ID是彻底的。

根据日志,看起来我的接收器永远不会启动。

将节目添加到电视提供商的类:

private static final Uri BASE_URI = Uri.parse(SCHEME + "://" + APPS_LAUNCH_HOST);
public static final String START_APP_ACTION_PATH = "startapp";
public static final String PLAY_VIDEO_ACTION_PATH = "playvideo";

public void addProgram(Movie movie) {
    ...
    PreviewProgram.Builder builder = new PreviewProgram.Builder()
            .setChannelId(channelId)
            .setTitle(movie.getTitle())
            .setDescription(movie.getDescription())
            .setDurationMillis(Long.valueOf(movie.getDuration()).intValue())
            .setType(TvContractCompat.PreviewPrograms.TYPE_MOVIE)
            .setIntentUri(Uri
                    .withAppendedPath(BASE_URI, PLAY_VIDEO_ACTION_PATH)
                    .buildUpon()
                    .appendPath(movieId)
                    .build())
            .setInternalProviderId(movieId)
            .setContentId(movieId)
    ...
}

public static long parseVideoId(Uri uri) {
    List<String> segments = uri.getPathSegments();
    if( segments.size() == 2 && PLAY_VIDEO_ACTION_PATH.equals(segments.get(0)) ) {
        return Long.valueOf(segments.get(1));
    }
    return -1L;
}

Android Manifest

<receiver android:name=".PlayVideoReceiver"
        android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.BROWSABLE" />
        <category android:name="android.intent.category.DEFAULT" />
        <data
            android:scheme="<scheme>"
            android:host="<host>"
            android:path="/playvideo" />
    </intent-filter>
</receiver>

1 个答案:

答案 0 :(得分:1)

我的清单有几个问题。

由于uri最终为scheme://host/playvideo/<id>,因此数据标记需要android:pathPrefix而不是android:path,因为路径将是动态的,而不仅仅是/playvideo

其次,主屏幕通过调用queryIntentForActivities()来搜索要启动的活动,因此我必须将我的接收器更改为解析Uri的活动并启动我的实际播放活动然后完成。

这就是我的清单应该是的。

<activity android:name=".channels.PlayVideoActivity">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.BROWSABLE" />
            <category android:name="android.intent.category.DEFAULT" />
            <data
                android:scheme="watchnextcodelab"
                android:host="com.example.android.watchnextcodelab"
                android:pathPrefix="/playvideo" />
        </intent-filter>
    </activity>