如何使通知灯在 android 上的 react-native 中工作? [英] How to make notification lights work in react-native on android?

查看:60
本文介绍了如何使通知灯在 android 上的 react-native 中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a simple react-native camera app and I would like to make notification LED light up at the bottom of the phone, when app is recording. I was not able to find it on the official docs.

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

I removed unnecessary code (like the styles and template) for ease of readability. My index.android.js is the following.

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 如果你需要它:

My package.json if you need it:

{
  "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 教程官方文档.

As you point out, this functionality is not included in RN, but the good thing is you can easily implement it yourself in Android code. Maybe something like this can help you turn the LED on/off (by basically creating a dummy notification) and then you can build an Android module, which is actually fairly simple. You can have a look at the Toast tutorial in the official docs.

这篇关于如何使通知灯在 android 上的 react-native 中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆