如何使通知灯在android上的react-native中工作?

时间:2016-11-18 17:52:14

标签: android react-native

我有一个简单的反应本机相机应用程序,我想在应用程序正在录制时,通知LED在手机底部亮起。我无法在官方docs上找到它。

为了便于阅读,我删除了不必要的代码(如样式和模板)。我的index.android.js如下。

import React from 'react';
import {
  View,
  Image,
  StatusBar,
  StyleSheet,
  AppRegistry,
  TouchableOpacity,
} from 'react-native';

import Camera from 'react-native-camera';

const styles = StyleSheet.create({
    //...
});

export default class DashCam extends React.Component {
  constructor(props) {
    super(props);

    this.camera = null;

    this.state = {
      camera: {
        aspect: Camera.constants.Aspect.fill,
        captureTarget: Camera.constants.CaptureTarget.cameraRoll,
        type: Camera.constants.Type.back,
        orientation: Camera.constants.Orientation.auto,
      },
      isRecording: false
    };
    this.switchCam = this.switchCam.bind(this);
    this.recording = this.recording.bind(this);
  }

  recording() {
    console.log(!this.state.isRecording);
    if(!this.state.isRecording) {
      if (this.camera) {
        this.camera.capture({mode: Camera.constants.CaptureMode.video})
            .then((data) => console.log(data))
            .catch(err => console.error(err));

        this.setState({ isRecording: true });
      }
      console.log('recording');
    } else {
      if (this.camera) {
        this.camera.stopCapture();
        this.setState({ isRecording: false });
      }
      console.log('stopped ');
    }
  }

  switchCam() {
    //...
  }

  get typeIcon() {
    //...
  }

  get camButton() {
    //...
  }

  render() {
    return (
        //...
    );
  }
}

AppRegistry.registerComponent('DashCam', () => DashCam);

如果您需要,我的package.json

{
  "name": "DashCam",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "jest"
  },
  "dependencies": {
    "react": "15.3.2",
    "react-native": "0.37.0",
    "react-native-camera": "git+https://github.com/lwansbrough/react-native-camera.git"
  },
  "jest": {
    "preset": "jest-react-native"
  },
  "devDependencies": {
    "babel-jest": "17.0.2",
    "babel-preset-react-native": "1.9.0",
    "jest": "17.0.3",
    "jest-react-native": "17.0.3",
    "react-test-renderer": "15.3.2"
  }
}

1 个答案:

答案 0 :(得分:1)

正如您所指出的,此功能不包含在RN中,但好处是您可以在Android代码中轻松实现它。也许像this之类的东西可以帮助你打开/关闭LED(通过基本上创建一个虚拟通知),然后你可以构建一个Android模块,这实际上非常简单。您可以查看官方文档中的Toast tutorial