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



我有一个简单的反应原生相机应用程序,我想使通知LED灯在手机底部,当应用程序正在录制。我没能在官方文档中找到它。

为了便于阅读,我删除了不必要的代码(比如样式和模板)。我的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"
  }
}

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

相关内容

  • 没有找到相关文章

最新更新