前言:
之前調整權限的方式請看這裡。這次調用權限的方式比較不一樣。看圖吧!
權限使用:
請參考這篇:https://yanlu.me/android-m6-0-permission-chasm/
以下全部複製。
這是WRITE_SETTINGS權限的使用方式。
記得先在manifest內增加權限
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
/** * An app can use this method to check if it is currently allowed to write or modify system * settings. In order to gain write access to the system settings, an app must declare the * {@link android.Manifest.permission#WRITE_SETTINGS} permission in its manifest. If it is * currently disallowed, it can prompt the user to grant it this capability through a * management UI by sending an Intent with action * {@link android.provider.Settings#ACTION_MANAGE_WRITE_SETTINGS}. * * @param context A context * @return true if the calling app can write to system settings, false otherwise */ if(!Settings.System.canWrite(this)){ Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + getPackageName())); startActivityForResult(intent, REQUEST_CODE); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE) { if (Settings.System.canWrite(this)) { //检查返回结果 Toast.makeText(MainActivity.this, "WRITE_SETTINGS permission granted", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "WRITE_SETTINGS permission not granted", Toast.LENGTH_SHORT).show(); } } }其中比較需要注意的地方是Settings.System.canWrite這方法在太低版本的Android上面不適用。
調用會Crash。
所以我們稍加做了一些判斷。
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {//android6.0以上使用需要權限 if (!Settings.System.canWrite(context)) {//沒有給予權限的話Do Something //跳轉畫面到權限頁面給使用者勾選 Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + getPackageName())); startActivityForResult(intent, 9487); } else {//已有權限 Do Something start_bRight();//開始調整螢幕 } } else {//android6.0以下可直接調用 start_bRight();//開始調整螢幕 } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case 9487: if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) { if (Settings.System.canWrite(this)) { start_bRight();//開始調整螢幕 } else { Toast.makeText(MainActivity.this, "請給予權限。", Toast.LENGTH_SHORT).show(); } } else { start_bRight();//開始調整螢幕 } break; } }
使用
Build.VERSION.SDK_INT > Build.VERSION_CODES.M
來判斷是否Android的版本為SDK 23以上,如果是23以下就沒有權限問題,也可以順利調用Settings.System.canWrite方法。
調整亮度Code:
裡面帶有SeekBar參數。//調整螢幕亮度 private void start_bRight() { //設定最大值 SeekBar m_SeekBar = (SeekBar) findViewById(R.id.see_bar); m_SeekBar.setMax(255); //取得目前系統的亮度值 int brightness = android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS, 255); Log.w(TAG, "亮度 素質" + brightness); //將亮度值 射定道系統中 m_SeekBar.setProgress(brightness); //關閉自動調整亮度功能 Settings.System.putInt(this.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); /* * @param cr要訪問的ContentResolver。 * @param name要修改的設置的名稱。 * @param value設置的新值。 */ //設定SeeBar的監聽事件 m_SeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { //觀察目前系統的亮度值 int brightness = android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS, 255); Log.w(TAG, "觀察亮度 素質" + brightness); //針對App端的UI作為 螢幕亮度調整 // (假設App端調整最暗,) Window localWindow = getWindow(); WindowManager.LayoutParams localLayoutParams = localWindow.getAttributes(); localLayoutParams.screenBrightness = progress / 255.0f; localWindow.setAttributes(localLayoutParams); //如果想改變整個系統的亮度 //方法1: // Uri uri = android.provider.Settings.System.getUriFor("screen_brightness"); // android.provider.Settings.System.putInt(context.getContentResolver(), getContentResolver(), progress); // context.getContentResolver().notifyChange(uri, null); //方法2: android.provider.Settings.System.putInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS, progress); /*補充:android.provider.Settings.System.putInt(參數1,參數2,參數3); * *參數1:需要一個ContentResolver ,所以getContentResolver() *參數2:其實就是一個字串,"screen_brightness",但是這個類別中已經有final String 位置在→ android.provider.Settings.System.SCREEN_BRIGHTNESS *參數3:設定的值 * * */ } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); }這裡還有另外一些調整亮度的方式。
如果你要進入畫面直接最亮可以嘗試以下方法
【Android開發經驗】與屏幕亮度調節相關的各種方法整理
public class SystemManager { private Context mContext; private static SystemManager sInstance; private SystemManager(final Context context) { mContext = context; } public static SystemManager init(final Context context) { if (null == sInstance) { sInstance = new SystemManager(context); } return sInstance; } public static SystemManager getInstance() { return sInstance; } // 判斷是否是自動調光模式 public boolean isAutoBrightness() { boolean automicBrightness = false; try { ContentResolver resolver = mContext.getContentResolver(); automicBrightness = Settings.System.getInt(resolver, Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC; } catch (SettingNotFoundException e) { e.printStackTrace(); } return automicBrightness; } // 設置屏幕亮度 public void setBrightness(Activity activity, int brightness) { WindowManager.LayoutParams lp = activity.getWindow().getAttributes(); lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f); activity.getWindow().setAttributes(lp); } // 保存屏幕亮度 public void saveBrightness(int brightness) { ContentResolver resolver = mContext.getContentResolver(); Uri uri = android.provider.Settings.System .getUriFor("screen_brightness"); android.provider.Settings.System.putInt(resolver, "screen_brightness", brightness); resolver.notifyChange(uri, null); } // 開啟自動調光模式 public void startAutoBrightness() { ContentResolver resolver = mContext.getContentResolver(); Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC); Uri uri = android.provider.Settings.System .getUriFor("screen_brightness"); resolver.notifyChange(uri, null); } // 關閉自動調光模式 public void stopAutoBrightness() { ContentResolver resolver = mContext.getContentResolver(); Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); Uri uri = android.provider.Settings.System .getUriFor("screen_brightness"); resolver.notifyChange(uri, null); } // 獲得當前屏幕亮度 public int getScreenBrightness() { int nowBrightnessValue = 0; try { ContentResolver resolver = mContext.getContentResolver(); nowBrightnessValue = android.provider.Settings.System.getInt( resolver, Settings.System.SCREEN_BRIGHTNESS); } catch (Exception e) { e.printStackTrace(); } return nowBrightnessValue; } // 設置光亮模式 public void setBrightnessMode(int mode) { Settings.System.putInt(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, mode); } // 獲得亮度模式 public int getBrightnessMode() { try { return Settings.System.getInt(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE); } catch (SettingNotFoundException e) { return Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC; } } //調到最亮 0~255 最暗~最亮 Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 255); }
Demo:
https://dl.dropboxusercontent.com/s/hcv9hqoaudd8wuk/ScreenBright.7z