下面列出了怎么用android.media.tv.TvContract.Channels的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Returns the current list of channels your app provides.
*
* @param resolver Application's ContentResolver.
* @return List of channels.
*/
public static List<Channel> getChannels(ContentResolver resolver) {
List<Channel> channels = new ArrayList<>();
// TvProvider returns programs in chronological order by default.
Cursor cursor = null;
try {
cursor = resolver.query(Channels.CONTENT_URI, Channel.PROJECTION, null, null, null);
if (cursor == null || cursor.getCount() == 0) {
return channels;
}
while (cursor.moveToNext()) {
channels.add(Channel.fromCursor(cursor));
}
} catch (Exception e) {
Log.w(TAG, "Unable to get channels", e);
} finally {
if (cursor != null) {
cursor.close();
}
}
return channels;
}
/**
* Returns the current list of channels your app provides.
*
* @param resolver Application's ContentResolver.
* @return List of channels.
*/
public static List<Channel> getChannels(ContentResolver resolver) {
List<Channel> channels = new ArrayList<>();
// TvProvider returns programs in chronological order by default.
Cursor cursor = null;
try {
cursor = resolver.query(Channels.CONTENT_URI, Channel.PROJECTION, null, null, null);
if (cursor == null || cursor.getCount() == 0) {
return channels;
}
while (cursor.moveToNext()) {
channels.add(Channel.fromCursor(cursor));
}
} catch (Exception e) {
Log.w(TAG, "Unable to get channels", e);
} finally {
if (cursor != null) {
cursor.close();
}
}
return channels;
}
public static LongSparseArray<Channel> buildChannelMap(ContentResolver resolver,
String inputId, List<Channel> channels) {
Uri uri = TvContract.buildChannelsUriForInput(inputId);
String[] projection = {
TvContract.Channels._ID,
TvContract.Channels.COLUMN_DISPLAY_NUMBER
};
LongSparseArray<Channel> channelMap = new LongSparseArray<>();
Cursor cursor = null;
try {
cursor = resolver.query(uri, projection, null, null, null);
if (cursor == null || cursor.getCount() == 0) {
return null;
}
while (cursor.moveToNext()) {
long channelId = cursor.getLong(0);
Log.d(TAG, "BUILD CHANNELS FOR "+channelId);
String channelNumber = cursor.getString(1);
channelMap.put(channelId, getChannelByNumber(channelNumber, channels));
}
} catch (Exception e) {
Log.d(TAG, "Content provider query: " + e.getStackTrace());
return null;
} finally {
if (cursor != null) {
cursor.close();
}
}
return channelMap;
}