查询程序返回安全例外

时间:2017-10-30 09:03:32

标签: android android-contentresolver android-tv

当我尝试通过通道或程序ID以外的任何其他方式查询程序数据库时,我得到以下异常:

java.lang.SecurityException: Selection not allowed for content://android.media.tv/program
    at android.os.Parcel.readException(Parcel.java:1683)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
    at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
    at android.content.ContentResolver.query(ContentResolver.java:530)
    at android.content.ContentResolver.query(ContentResolver.java:472)

请注意,我可以添加和删除程序,因此我认为在清单文件中正确设置了权限。

以下示例代码尝试按程序标题进行查询,但抛出了上述异常。

Uri uri = TvContract.Programs.CONTENT_URI;

Cursor cursor = null;
try {
    String mSelectionClause = TvContract.Programs.COLUMN_TITLE + " = ?";
    String[] mSelectionArgs = { "Test title" };
    cursor = resolver.query(uri, Program.PROJECTION, mSelectionClause, mSelectionArgs, null);
    if (cursor == null || cursor.getCount() == 0) {
        Log.d(TAG, "No programs found for 'Test title'");
        return programs;
    }
    Log.d(TAG, cursor.getCount() + " programs found for 'Test tilte'");
} catch (Exception e) {
    Log.w(TAG, "Unable to get programs for 'Test title'", e);
} finally {
    if (cursor != null) {
        cursor.close();
    }
}

是否无法通过任何列进行查询?

1 个答案:

答案 0 :(得分:2)

TvProvider未开启供选择。您可以添加/更新/删除但无法创建自定义查询。您需要使用TvContractCompat来构建查询URI并评估结果。

context.getContentResolver()
    .query(TvContractCompat.buildChannelUri(channelId), null, null, null, null);

或程序

context.getContentResolver()
    .query(TvContractCompat.buildProgramsUriForChannel(channelId), null, null, null, null);

和特定程序

context.getContentResolver()
    .query(TvContractCompat.buildProgramUri(programId), null, null, null, null);

对于您的示例,您应该查询频道中的节目并搜索标题。

try (Cursor cursor = context.getContentResolver()
                      .query(
                TvContractCompat.buildProgramsUriForChannel(channelId),
                null, null, null, null)) {
    if (cursor != null && cursor.moveToNext()) {
        Program program = Program.fromCursor(cursor);
        if( "Test Title".equals(program.getTitle()) {
            // Do stuff...
        }
    }
}

参考: https://developer.android.com/training/tv/discovery/recommendations-channel.html#best_practices