You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PhoneGap应用前台运行时保持屏幕常亮的解决方案咨询

Fixing Screen Keep-On for Your PhoneGap App

Hey there! I know how frustrating it is when your app keeps locking even after trying the usual native Android tricks. Let’s go through some alternative, PhoneGap-specific solutions that might do the trick:

1. Use a Cordova Wake Lock Plugin (Most Reliable)

Since PhoneGap is built on Cordova, leveraging a well-maintained plugin is often the easiest way to handle this without diving too deep into native code. Two popular options are cordova-plugin-insomnia and cordova-plugin-wakelock:

  • First, install the plugin via CLI:
    cordova plugin add cordova-plugin-insomnia
    
  • Then, in your app’s JavaScript code, trigger the wake lock when the app is in the foreground (you can tie this to the deviceready event or a foreground state listener):
    document.addEventListener('deviceready', function() {
        // Keep screen awake while app is active
        window.plugins.insomnia.keepAwake();
    
        // Optional: Release lock when app goes to background
        document.addEventListener('pause', function() {
            window.plugins.insomnia.allowSleepAgain();
        });
    
        // Re-enable when app comes back to foreground
        document.addEventListener('resume', function() {
            window.plugins.insomnia.keepAwake();
        });
    }, false);
    

Plugins handle the native lifecycle events correctly, so this avoids common pitfalls with manual native code implementations.

2. Configure via config.xml (No Code Needed)

Cordova has a built-in preference for keeping the screen on that you might have missed. Add this line to your project’s config.xml under the Android platform section:

<platform name="android">
    <!-- Other Android configs -->
    <preference name="KeepScreenOn" value="true" />
</platform>

This automatically sets the FLAG_KEEP_SCREEN_ON flag on the app’s WebView, which is the core view of your PhoneGap app. It’s a clean, declarative approach that works for most cases.

3. Target the WebView Directly in Native Code

If you still want to stick with native Java code, try applying the screen-on flag directly to the PhoneGap WebView instead of the Activity. Sometimes the Activity’s flags get overridden by the WebView’s state:

In your MainActivity.java:

import org.apache.cordova.CordovaWebView;

public class MainActivity extends CordovaActivity {
    private CordovaWebView appView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        appView = this.appView; // Get the PhoneGap WebView instance
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (appView != null) {
            appView.setKeepScreenOn(true); // Enable screen lock on resume
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (appView != null) {
            appView.setKeepScreenOn(false); // Disable when app is paused
        }
    }
}

This ensures the flag is applied directly to the view that’s actually visible to the user.

4. Double-Check Permissions

If you tried using WakeLock earlier and it didn’t work, make sure you’ve added the required permission to your AndroidManifest.xml:

<uses-permission android:name="android.permission.WAKE_LOCK" />

Without this permission, the WakeLock will fail silently—no errors, just no screen-on behavior.

A quick tip: Always test the app’s behavior when switching between foreground and background to make sure you’re not keeping the screen awake unnecessarily (which drains battery).

内容的提问来源于stack exchange,提问作者Pbj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:33:56