Developing a TV Input Service

Previous Next

This lesson teaches you to

  1. Declare Your TV Input Service in the Manifest
  2. Define Your TV Input Service
  3. Define Your Setup Activity

You should also read

  • android.media.tv
  • TV Input Framework

Try It Out

  • TV Input Service sample app

A TV input service represents a media stream source, and lets you present your media content in a linear, broadcast TV fashion as channels and programs. With the TV input service, you can provide parental controls, program guide information, and content ratings. The TV input service works with the Android system TV app, developed for the device and immutable by third-party apps, which ultimately controls and presents content on the TV. See TV Input Framework for more information about the framework architecture and its components.

To develop a TV input service, you implement the following components:

  • TvInputService provides long-running and background availability for the TV input
  • TvInputService.Session maintains the TV input state and communicates with the hosting app
  • TvContract describes the channels and programs available to the TV input
  • TvContract.Channels represents information about a TV channel
  • TvContract.Programs describes a TV program with data such as program title and start time
  • TvTrackInfo represents an audio, video, or subtitle track
  • TvContentRating describes a content rating, allows for custom content rating schemes
  • TvInputManager provides an API to the system TV app and manages the interaction with TV inputs and apps

Declare Your TV Input Service in the Manifest



Your app manifest must declare your TvInputService. Within that declaration, specify the BIND_TV_INPUTpermission to allow the service to connect the TV input to the system. A system service (TvInputManagerService) performs the binding and has that permission. The system TV app sends requests to TV input services via the TvInputManager interface. The service declaration must also include an intent filter that specifies the TvInputService as the action to perform with the intent. Also within the service declaration, declare the service meta data in a separate XML resource. The service declaration, the intent filter and the service meta data are described in the following example.

 android:name="com.example.sampletvinput.SampleTvInput"    android:label="@string/sample_tv_input_label"    android:permission="android.permission.BIND_TV_INPUT">             android:name="android.media.tv.TvInputService" />         android:name="android.media.tv.input"      android:resource="@xml/sample_tv_input" />

Define the service meta data in separate XML file, as shown in the following example. The service meta data must include a setup interface that describes the TV input's initial configuration and channel scan. The service meta data file is located in the XML resources directory for your application and must match the name of the resource in the manifest. Using the example manifest entries above, you would create an XML file in the locationres/xml/sample_tv_input.xml, with the following contents:

 xmlns:android="http://schemas.android.com/apk/res/android"  android:setupActivity="com.example.sampletvinput.SampleTvInputSetupActivity" />

Define Your TV Input Service

Android TV -5.1- Developing a TV Input Service_第1张图片


Figure 1.TvInputService lifecycle.

For your service, you extend the TvInputService class. A TvInputServiceimplementation is a bound service where the system service (TvInputManagerService) is the client that binds to it. The service life cycle methods you need to implement are illustrated in figure 1.

The onCreate() method initializes and starts the HandlerThread which provides a process thread separate from the UI thread to handle system-driven actions. In the following example, the onCreate() method initializes the CaptioningManager and prepares to handle theACTION_BLOCKED_RATINGS_CHANGED andACTION_PARENTAL_CONTROLS_ENABLED_CHANGED actions. These actions describe system intents fired when the user changes the parental control settings, and when there is a change on the list of blocked ratings.

@Overridepublic void onCreate() {    super.onCreate();    mHandlerThread = new HandlerThread(getClass()      .getSimpleName());    mHandlerThread.start();    mDbHandler = new Handler(mHandlerThread.getLooper());    mHandler = new Handler();    mCaptioningManager = (CaptioningManager)      getSystemService(Context.CAPTIONING_SERVICE);    setTheme(android.R.style.Theme_Holo_Light_NoActionBar);    mSessions = new ArrayList<BaseTvInputSessionImpl>();    IntentFilter intentFilter = new IntentFilter();    intentFilter.addAction(TvInputManager      .ACTION_BLOCKED_RATINGS_CHANGED);    intentFilter.addAction(TvInputManager      .ACTION_PARENTAL_CONTROLS_ENABLED_CHANGED);    registerReceiver(mBroadcastReceiver, intentFilter);}

See Control Content for more information about working with blocked content and providing parental control. See TvInputManager for more system-driven actions that you may want to handle in your TV input service.

The TvInputService creates a TvInputService.Session that implements Handler.Callback to handle player state changes. With onSetSurface(), the TvInputService.Session sets the Surface with the video content. See Integrate Player with Surface for more information about working with Surface to render video.

The TvInputService.Session handles the onTune() event when the user selects a channel, and notifies the system TV app for changes in the content and content meta data. These notify() methods are described inControl Content and Handle Track Selection further in this training.

Define Your Setup Activity



The system TV app works with the setup activity you define for your TV input. The setup activity is required and must provide at least one channel record for the system database. The system TV app will invoke the setup activity when it cannot find a channel for the TV input.

The setup activity describes to the system TV app the channels made available through the TV input, as demonstrated in the next lesson, Creating and Updating Channel Data.

Next: Working with Channel Data Get news & tips 

更多相关文章

  1. Android 原始下载图片 通过HTTp
  2. Android:下载网络图片
  3. android ScrollView 多张图片之间有空白
  4. android从网上下载图片
  5. Android图片上传工具类
  6. Android 自定义控件之图片裁剪
  7. android访问网络图片
  8. 【Android】简单图片浏览器

随机推荐

  1. android heapStartSize heapMaximumSize
  2. android fastboot flash system 失败
  3. Android APK分析工具
  4. persist应用
  5. Android面试题目总结
  6. Android无法启动虚拟机
  7. Android音视频开发学习笔记
  8. SMS短信发送
  9. android x86 Download、 Buildnig and Ru
  10. Android中给图片加边框