如何在SettingsPreferences.java调用CheckBoxPreference的key并实现应用亮屏常驻
Hey there! Let's work through your two Android dev needs step by step—making the screen stay on when your CheckBoxPreference is enabled, and accessing that preference's key in your SettingsPreferences.java file.
1. Implement "Keep Screen On" Functionality
First, let's make sure your setting_preferences.xml has a proper key for the CheckBoxPreference (you’ll need this to reference its state later). Here’s the complete XML with the missing key attribute:
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <CheckBoxPreference android:key="pref_keep_screen_on" android:title="@string/screen_on_off" android:summary="@string/keep_screen_on_selection" android:defaultValue="false"/> </PreferenceScreen>
Next, update your MainActivity.java to check the preference state on launch and whenever the activity resumes (so it works if the app comes back from the background):
import android.os.Bundle; import android.preference.PreferenceManager; import android.view.WindowManager; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Check preference state and apply screen setting updateScreenKeepOnState(); } @Override protected void onResume() { super.onResume(); // Re-check when activity comes back to foreground updateScreenKeepOnState(); } private void updateScreenKeepOnState() { // Get the default shared preferences boolean keepScreenOn = PreferenceManager.getDefaultSharedPreferences(this) .getBoolean("pref_keep_screen_on", false); if (keepScreenOn) { // Enable screen keep on getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); } else { // Disable screen keep on getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); } } }
This code reads the preference value and toggles the FLAG_KEEP_SCREEN_ON window flag accordingly—no special permissions needed for this!
2. Access the CheckBoxPreference Key in SettingsPreferences.java
Assuming your SettingsPreferences.java is a subclass of PreferenceFragmentCompat (the standard for modern Android settings), here’s how you can reference the CheckBoxPreference using its key:
Option 1: Get the Preference Instance
You can fetch the CheckBoxPreference directly to modify it or set a listener:
import android.os.Bundle; import androidx.preference.CheckBoxPreference; import androidx.preference.PreferenceFragmentCompat; public class SettingsPreferences extends PreferenceFragmentCompat { @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { setPreferencesFromResource(R.xml.setting_preferences, rootKey); // Get the CheckBoxPreference using its key CheckBoxPreference keepScreenOnPref = findPreference("pref_keep_screen_on"); // Example: Set a listener to react when the preference is toggled if (keepScreenOnPref != null) { keepScreenOnPref.setOnPreferenceChangeListener((preference, newValue) -> { Boolean isChecked = (Boolean) newValue; // You can add immediate actions here if needed return true; }); } } }
Option 2: Read the Preference Value Directly
If you just need to check the current state of the preference, you can use SharedPreferences directly in SettingsPreferences.java:
import android.preference.PreferenceManager; // ... inside your SettingsPreferences class boolean isKeepScreenOnEnabled = PreferenceManager.getDefaultSharedPreferences(requireContext()) .getBoolean("pref_keep_screen_on", false);
内容的提问来源于stack exchange,提问作者Ashok

