2016-11-18 26 views
2

我有一個簡單的反應本機相機應用程序,我想通知LED指示燈在手機的底部,當應用程序正在錄製。我無法在官方docs上找到它。如何使通知燈在Android上的react-native工作?

爲了便於閱讀,我刪除了不必要的代碼(如樣式和模板)。我的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

正如你指出,這個功能不包括在RN,但好處是,你可以很容易地實現它自己在Android中的代碼。也許像this可以幫助你打開/關閉LED(通過基本上創建一個虛擬通知),然後你可以建立一個Android模塊,這實際上很簡單。您可以在官方文檔中查看Toast tutorial

+0

感謝您的信息,我會檢查出來!我會在你寫的模塊上寫一個模塊。 – user000001

+1

我要添加兩個鏈接:https://www.sitepoint.com/access-platform-apis-with-react-native-modules/ http://www.41post.com/4706/programming/android -creating-A-兩色爲主導的通知 – user000001