您好,登錄后才能下訂單哦!
本文實(shí)例講述了Android開(kāi)發(fā)實(shí)現(xiàn)調(diào)節(jié)屏幕亮度功能。分享給大家供大家參考,具體如下:
在很多app中進(jìn)入二維碼顯示界面時(shí)會(huì)自動(dòng)調(diào)整屏幕亮度,那么如何實(shí)現(xiàn)調(diào)節(jié)app的屏幕亮度呢?下面我來(lái)為大家介紹:
注:調(diào)節(jié)屏幕亮度的核心思想就是對(duì)安卓系統(tǒng)提供的ContentProvider
進(jìn)行操作
1、聲明權(quán)限
需要允許用戶修改系統(tǒng)配置
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"/> <uses-permission android:name="android.permission.WRITE_SETTINGS"/>
2、具體操作
/** * 判斷是否開(kāi)啟了自動(dòng)亮度調(diào)節(jié) */ public static boolean isAutoBrightness(Context context) { ContentResolver resolver = context.getContentResolver(); boolean automicBrightness = false; try { automicBrightness = Settings.System.getInt(resolver, Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC; } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } return automicBrightness; } /** * 獲取屏幕的亮度 */ public static int getScreenBrightness(Context context) { int nowBrightnessValue = 0; ContentResolver resolver = context.getContentResolver(); try { nowBrightnessValue = android.provider.Settings.System.getInt(resolver, Settings.System.SCREEN_BRIGHTNESS); } catch (Exception e) { e.printStackTrace(); } return nowBrightnessValue; } /** * 設(shè)置當(dāng)前Activity顯示時(shí)的亮度 * 屏幕亮度最大數(shù)值一般為255,各款手機(jī)有所不同 * screenBrightness 的取值范圍在[0,1]之間 */ public static void setBrightness(Activity activity, int brightness) { WindowManager.LayoutParams lp = activity.getWindow().getAttributes(); lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f); activity.getWindow().setAttributes(lp); } /** * 開(kāi)啟關(guān)閉自動(dòng)亮度調(diào)節(jié) */ public static boolean autoBrightness(Context activity, boolean flag) { int value = 0; if (flag) { value = Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC; //開(kāi)啟 } else { value = Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;//關(guān)閉 } return Settings.System.putInt(activity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, value); } /** * 保存亮度設(shè)置狀態(tài),退出app也能保持設(shè)置狀態(tài) */ public static void saveBrightness(Context context, int brightness) { ContentResolver resolver = context.getContentResolver(); Uri uri = android.provider.Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS); android.provider.Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS, brightness); resolver.notifyChange(uri, null); }
好了,亮度調(diào)節(jié)差不多就這樣了。
另外,更多關(guān)于Android權(quán)限控制的說(shuō)明可點(diǎn)擊此處查看Android權(quán)限操作說(shuō)明
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開(kāi)發(fā)入門與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。