Prevent the phone screen from going to sleep.
0

Configure Feed

Select the types of activity you want to include in your feed.

feat: implement keep awake on Android/iOS

Joseph Hale (Jul 25, 2025, 4:05 PM -0700) d788f88d 6966a6b8

+65 -33
+18 -12
android/src/main/java/com/keepawake/KeepAwakeModule.kt
··· 1 1 package com.keepawake 2 2 3 + import android.util.Log 4 + import android.view.WindowManager 3 5 import com.facebook.react.bridge.ReactApplicationContext 4 6 import com.facebook.react.module.annotations.ReactModule 5 - import android.util.Log 6 7 7 8 @ReactModule(name = KeepAwakeModule.NAME) 8 9 class KeepAwakeModule(reactContext: ReactApplicationContext) : 9 10 NativeKeepAwakeSpec(reactContext) { 10 11 12 + private val context = reactContext 13 + 11 14 override fun getName(): String { 12 15 return NAME 13 16 } 14 17 15 - 18 + /** 19 + * Stops the screen from dimming or locking 20 + */ 16 21 override fun activate() { 17 - // Implementation to keep the device awake 18 - Log.d(NAME, "activate") 22 + Log.d(NAME, "Activating KeepAwake") 23 + context.currentActivity?.runOnUiThread { 24 + context.currentActivity?.window?.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) 25 + } 19 26 } 20 27 28 + /** 29 + * Allows the screen to dim or lock 30 + */ 21 31 override fun deactivate() { 22 - // Implementation to allow the device to sleep 23 - Log.d(NAME, "deactivate") 24 - } 25 - 26 - override fun isActive(): Boolean { 27 - // Implementation to check if the device is set to stay awake 28 - Log.d(NAME, "isActive") 29 - return false 32 + Log.d(NAME, "Deactivating KeepAwake") 33 + context.currentActivity?.runOnUiThread { 34 + context.currentActivity?.window?.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) 35 + } 30 36 } 31 37 32 38 companion object {
+37 -6
example/src/App.tsx
··· 1 1 import * as KeepAwake from 'react-native-keep-awake'; 2 2 3 - import { StyleSheet, Text, View } from 'react-native'; 3 + import { Button, Platform, StyleSheet, Text, View } from 'react-native'; 4 4 5 5 export default function App() { 6 - KeepAwake.activate(); 7 - KeepAwake.deactivate(); 8 - const result = KeepAwake.isActive(); 9 - console.log('KeepAwake is active:', result); 10 6 return ( 11 7 <View style={styles.container}> 12 - <Text>Is KeepAwake active? {result ? 'yes' : 'no'}</Text> 8 + <View style={styles.instructions}> 9 + <Text>HOW TO USE THIS DEMO</Text> 10 + <Text>1. Turn up your screen brightness</Text> 11 + <Text> 12 + 2. Change your device display settings to dim as quickly as possible 13 + </Text> 14 + <Text> 15 + 3. Have fun using the buttons below to control whether or not your 16 + device dims/sleeps quickly! 17 + </Text> 18 + {Platform.OS === 'ios' && ( 19 + <Text> 20 + Note: On iOS, you may need to disconnect your device from XCode to 21 + see the effects 22 + </Text> 23 + )} 24 + </View> 25 + <Button 26 + title="Activate" 27 + onPress={() => { 28 + KeepAwake.activate(); 29 + }} 30 + /> 31 + <Button 32 + title="Deactivate" 33 + onPress={() => { 34 + KeepAwake.deactivate(); 35 + }} 36 + /> 13 37 </View> 14 38 ); 15 39 } ··· 19 43 backgroundColor: '#fff', 20 44 flex: 1, 21 45 alignItems: 'center', 46 + justifyContent: 'flex-start', 47 + gap: 20, 48 + }, 49 + instructions: { 50 + margin: 20, 51 + alignItems: 'flex-start', 22 52 justifyContent: 'center', 53 + marginBottom: 20, 23 54 }, 24 55 });
+10 -10
ios/KeepAwake.mm
··· 5 5 6 6 7 7 - (void)activate { 8 - // Implementation for activating the keep awake functionality 9 - NSLog(@"KeepAwake activated"); 8 + // Stops the screen from dimming or locking 9 + NSLog(@"Activating KeepAwake"); 10 + dispatch_async(dispatch_get_main_queue(), ^{ 11 + [[UIApplication sharedApplication] setIdleTimerDisabled:YES]; 12 + }); 10 13 } 11 14 12 15 - (void)deactivate { 13 - // Implementation for deactivating the keep awake functionality 14 - NSLog(@"KeepAwake deactivated"); 15 - } 16 - 17 - - (BOOL)isActive { 18 - // Implementation to check if the keep awake functionality is active 19 - NSLog(@"Checking if KeepAwake is active"); 20 - return NO; 16 + // Allows the screen to dim or lock 17 + NSLog(@"Deactivating KeepAwake"); 18 + dispatch_async(dispatch_get_main_queue(), ^{ 19 + [[UIApplication sharedApplication] setIdleTimerDisabled:NO]; 20 + }); 21 21 } 22 22 23 23 - (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
-1
src/NativeKeepAwake.ts
··· 4 4 export interface Spec extends TurboModule { 5 5 activate(): void; 6 6 deactivate(): void; 7 - isActive(): boolean; 8 7 } 9 8 10 9 export default TurboModuleRegistry.getEnforcing<Spec>('KeepAwake');
-4
src/index.tsx
··· 7 7 export function deactivate(): void { 8 8 KeepAwake.deactivate(); 9 9 } 10 - 11 - export function isActive(): boolean { 12 - return KeepAwake.isActive(); 13 - }