以程式方式修改系統亮度
要以程式調整設備螢幕的亮度,您可以使用各種方法。常見的方法是處理螢幕亮度屬性:
<code class="java">WindowManager.LayoutParams lp = window.getAttributes(); lp.screenBrightness = (float)brightness; window.setAttributes(lp);</code>
但是,由於潛在的限製或不正確的最大值設置,此技術可能並不總是有效。
另一種方法是利用系統設定和ContentResolver:
<code class="java">private ContentResolver cResolver; private Window window; private int brightness; onCreate() { cResolver = getContentResolver(); window = getWindow(); try { // Handle auto brightness mode Settings.System.putInt(cResolver, Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); // Retrieve current brightness brightness = Settings.System.getInt(cResolver, Settings.System.SCREEN_BRIGHTNESS); } catch (SettingNotFoundException e) { Log.e("Error", "Cannot access system brightness"); e.printStackTrace(); } }</code>
然後您可以調整亮度:
<code class="java">// Update system brightness Settings.System.putInt(cResolver, Settings.System.SCREEN_BRIGHTNESS, brightness); // Update window brightness attributes LayoutParams layoutpars = window.getAttributes(); layoutpars.screenBrightness = brightness / 255f; window.setAttributes(layoutpars);</code>
不要忘記在清單中包含必要的權限:
<code class="xml"><uses-permission android:name="android.permission.WRITE_SETTINGS" /></code>
對於API 等級23 及以上,您需要按照文件中的說明透過Settings Activity 請求權限。
以上是如何在 Android 上以程式調整螢幕亮度?的詳細內容。更多資訊請關注PHP中文網其他相關文章!