Skip to main content
版本: 0.64

为电视和机顶盒制作应用

目前的 React Native 应用只需在 JavaScript 端简单修改甚至无需修改,在电视和机顶盒设备上就基本可用了。

编译修改#

  • 原生端: 在 Android TV 上运行 React Native 项目请先在AndroidManifest.xml中加入下列配置:
  <!-- 加入自定义的banner图作为TV设备上的图标 --> <application  ...  android:banner="@drawable/tv_banner"  >    ...    <intent-filter>      ...      <!-- Needed to properly create a launch intent when running on Android TV -->      <category android:name="android.intent.category.LEANBACK_LAUNCHER"/>    </intent-filter>    ...  </application>
  • JavaScript 端: 对于电视设备的检测代码已经加入到了Platform模块中。你可以使用下面的代码来检测当前运行设备是否是电视设备:
import { Platform } from 'react-native';const running_on_tv = Platform.isTV;

代码修改#

  • 访问可点击的控件: When running on Android TV the Android framework will automatically apply a directional navigation scheme based on relative position of focusable elements in your views. The Touchable mixin has code added to detect focus changes and use existing methods to style the components properly and initiate the proper actions when the view is selected using the TV remote, so TouchableWithoutFeedback, TouchableHighlight, TouchableOpacity and TouchableNativeFeedback will work as expected. In particular:

    • onFocus will be executed when the touchable view goes into focus
    • onBlur will be executed when the touchable view goes out of focus
    • onPress will be executed when the touchable view is actually selected by pressing the "select" button on the TV remote.
  • TV remote/keyboard input: A new native class, ReactAndroidTVRootViewHelper, sets up key events handlers for TV remote events. When TV remote events occur, this class fires a JS event. This event will be picked up by instances of the TVEventHandler JavaScript object. Application code that needs to implement custom handling of TV remote events can create an instance of TVEventHandler and listen for these events, as in the following code:

const TVEventHandler = require('TVEventHandler');
class Game2048 extends React.Component {  _tvEventHandler: any;
  _enableTVEventHandler() {    this._tvEventHandler = new TVEventHandler();    this._tvEventHandler.enable(this, function(cmp, evt) {      if (evt && evt.eventType === 'right') {        cmp.setState({board: cmp.state.board.move(2)});      } else if(evt && evt.eventType === 'up') {        cmp.setState({board: cmp.state.board.move(1)});      } else if(evt && evt.eventType === 'left') {        cmp.setState({board: cmp.state.board.move(0)});      } else if(evt && evt.eventType === 'down') {        cmp.setState({board: cmp.state.board.move(3)});      } else if(evt && evt.eventType === 'playPause') {        cmp.restartGame();      }    });  }
  _disableTVEventHandler() {    if (this._tvEventHandler) {      this._tvEventHandler.disable();      delete this._tvEventHandler;    }  }
  componentDidMount() {    this._enableTVEventHandler();  }
  componentWillUnmount() {    this._disableTVEventHandler();  }
  • Dev Menu support: On the emulator, cmd-M will bring up the developer menu, similar to Android. To bring it up on a real Android TV device, press the menu button or long press the fast-forward button on the remote. (Please do not shake the Android TV device, that will not work :) )

  • 已知问题: