安卓应用三种屏幕活动模式实现咨询:全局常亮及充电常亮等
Hey there! Let's walk through how to implement each of these screen timeout modes for your Android app, including fixing the global always-on issue you mentioned.
1. 全局实现“屏幕始终保持常亮”
You’re right that getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) only works for a single Activity. To make this apply across all your app’s screens, here are two reliable approaches:
方案一:自定义BaseActivity(推荐)
Create a parent BaseActivity that all your other Activities inherit from. Add the flag logic in its onCreate method:
public class BaseActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Check if the user has enabled the "always on" mode if (isAlwaysOnModeEnabled()) { getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); } } // Fetch the user's setting from SharedPreferences (or your preferred storage) private boolean isAlwaysOnModeEnabled() { SharedPreferences prefs = getSharedPreferences("AppSettings", MODE_PRIVATE); return prefs.getBoolean("screen_mode_always_on", false); } }
Then update every other Activity to extend BaseActivity instead of AppCompatActivity. This keeps your screen logic centralized and easy to maintain.
方案二:ActivityLifecycleCallbacks(无需修改继承关系)
If you don’t want to adjust your Activity inheritance chain, use Android’s ActivityLifecycleCallbacks in your Application class to hook into every Activity’s creation:
public class MyApp extends Application { @Override public void onCreate() { super.onCreate(); registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() { @Override public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) { if (isAlwaysOnModeEnabled()) { activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); } } // Leave the other lifecycle methods empty unless you need them @Override public void onActivityStarted(@NonNull Activity activity) {} @Override public void onActivityResumed(@NonNull Activity activity) {} @Override public void onActivityPaused(@NonNull Activity activity) {} @Override public void onActivityStopped(@NonNull Activity activity) {} @Override public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {} @Override public void onActivityDestroyed(@NonNull Activity activity) {} }); } private boolean isAlwaysOnModeEnabled() { SharedPreferences prefs = getSharedPreferences("AppSettings", MODE_PRIVATE); return prefs.getBoolean("screen_mode_always_on", false); } }
Don’t forget to declare your custom Application in AndroidManifest.xml:
<application android:name=".MyApp" <!-- other attributes --> >
2. 实现“充电时屏幕始终常亮”
This requires listening for changes in the device’s charging state and toggling the screen flag dynamically. Here’s how to set it up with BaseActivity:
public class BaseActivity extends AppCompatActivity { private BroadcastReceiver chargingReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setupChargingListener(); } private void setupChargingListener() { chargingReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // Only act if charging mode is enabled if (!isChargingModeEnabled()) return; int batteryStatus = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1); boolean isCharging = batteryStatus == BatteryManager.BATTERY_STATUS_CHARGING || batteryStatus == BatteryManager.BATTERY_STATUS_FULL; if (isCharging) { getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); } else { getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); } } }; // Register for the battery status broadcast (no permission needed for this system broadcast) IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); registerReceiver(chargingReceiver, filter); } @Override protected void onDestroy() { super.onDestroy(); // Clean up the receiver to avoid memory leaks if (chargingReceiver != null) { unregisterReceiver(chargingReceiver); } } private boolean isChargingModeEnabled() { SharedPreferences prefs = getSharedPreferences("AppSettings", MODE_PRIVATE); return prefs.getBoolean("screen_mode_charging_only", false); } }
This receiver will automatically adjust the screen flag whenever the charging state changes—no manual intervention needed.
3. 实现“遵循安卓系统默认设置”
This is the simplest mode: just ensure your app isn’t overriding the system’s screen timeout. Whenever the user selects this mode, clear any existing screen flags in all active Activities:
// Call this when the user switches to system default mode private void applySystemDefaultMode() { getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); }
To make this work across all screens, use an event bus like EventBus to notify every Activity when the mode changes. For example:
- Send an event when the user saves their setting:
EventBus.getDefault().post(new ScreenModeChangedEvent(ScreenMode.SYSTEM_DEFAULT));
- Subscribe to the event in BaseActivity to update the flag immediately:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EventBus.getDefault().register(this); } @Subscribe(threadMode = ThreadMode.MAIN) public void onScreenModeChanged(ScreenModeChangedEvent event) { if (event.getMode() == ScreenMode.SYSTEM_DEFAULT) { getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); } } @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); }
内容的提问来源于stack exchange,提问作者George Slupitsky

