React Native保持scree清醒

时间:2017-10-30 06:54:28

标签: reactjs react-native

我找到了一个模块react-native-keep-awake,用于让屏幕保持清醒状态以便做出反应。但是,看起来像这个模块有一些问题,我尝试console.log(this.props.screenShouldBeAwake);值,它给了我undefined。有没有其他方法可以触发屏幕醒来?

2 个答案:

答案 0 :(得分:0)

好的我有解决方案,希望将来帮助别人。

  1. 从'react-native-keep-awake'导入KeepAwake;
  2. 创建一个州并将其命名为keepawake,或任何你想要的名称。
  3. 创建一个箭头函数,以便在onPress

    时调用
    changeKeepAwake = () =>{
       if(!this.state.keepawake){
         this.state.keepawake = true;
         KeepAwake.activate();
       }else{
         this.state.keepawake = false;
         KeepAwake.deactivate();
       }
    

    }

  4. DONE

答案 1 :(得分:0)

现在,expo支持此功能。易于使用。

import { activateKeepAwake, deactivateKeepAwake } from 'expo-keep-awake';
// Prevents the screen from sleeping until deactivateKeepAwake is called with the same tag value.
activateKeepAwake("tag");
// Releases the lock on screen-sleep prevention associated with the given tag value. 
deactivateKeepAwake("tag");

expo doc

中查看详细信息