編輯:關於Android編程
本文實例講述了Android編程獲取並設置Activity亮度的方法。分享給大家供大家參考,具體如下:
此API只適合2.1以上版本:
/** * 判斷是否開啟了自動亮度調節 * * @param aContext * @return */ public static boolean isAutoBrightness(ContentResolver aContentResolver) { boolean automicBrightness = false; try { automicBrightness = Settings.System.getInt(aContentResolver, Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC; } catch (SettingNotFoundException e) { e.printStackTrace(); } return automicBrightness; } /** * 獲取屏幕的亮度 * * @param activity * @return */ public static int getScreenBrightness(Activity activity) { int nowBrightnessValue = 0; ContentResolver resolver = activity.getContentResolver(); try { nowBrightnessValue = android.provider.Settings.System.getInt( resolver, Settings.System.SCREEN_BRIGHTNESS); } catch (Exception e) { e.printStackTrace(); } return nowBrightnessValue; } /** * 設置亮度 * * @param activity * @param brightness */ public static void setBrightness(Activity activity, int brightness) { // Settings.System.putInt(activity.getContentResolver(), // Settings.System.SCREEN_BRIGHTNESS_MODE, // Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); WindowManager.LayoutParams lp = activity.getWindow().getAttributes(); lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f); activity.getWindow().setAttributes(lp); } /** * 停止自動亮度調節 * * @param activity */ public static void stopAutoBrightness(Activity activity) { Settings.System.putInt(activity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); } /** * 開啟亮度自動調節 * * @param activity */ public static void startAutoBrightness(Activity activity) { Settings.System.putInt(activity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC); } /** * 保存亮度設置狀態 * * @param resolver * @param brightness */ public static void saveBrightness(ContentResolver resolver, int brightness) { Uri uri = android.provider.Settings.System .getUriFor("screen_brightness"); android.provider.Settings.System.putInt(resolver, "screen_brightness", brightness); resolver.notifyChange(uri, null); }
使用SeekBar進行亮度控制:
sSeekBar = (SeekBar) findViewById(R.id.seek_bar); sSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { if (progress < 10) { } else { setBrightness(Test2Activity.this, progress); } } }); //獲取當前亮度的位置 int a =getScreenBrightness(this); sSeekBar.setProgress(a);
<SeekBar android:id="@+id/seek_bar" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:max="255" android:maxHeight="8dp" android:minHeight="8dp" android:paddingBottom="5dp" android:paddingLeft="13dp" android:paddingRight="13dp" android:progress="0" />
希望本文所述對大家Android程序設計有所幫助。
SlidingMenu是一個第三方的開源的側滑控件。是一種很好的交互邏輯。有很多優秀的應用使用了SlidingMenu例如QQ和CSDN的安卓客戶端 其gith
SparseArray是Android框架獨有的類,在標准的JDK中不存在這個類。它要比 HashMap 節省內存,某些情況下比HashMap性能更好,按照官方問答的解
本文實例為大家分享了Android中使用定時器的三種方法,供大家參考,具體內容如下圖示:因為都比較簡單,所以就直接貼代碼(慮去再次點擊停止的操作),有個全局的Handle
android 迭代開發中陸續遇到各種問題,我們要善於總結,歸類。現在記錄一下這幾個月遇到的問題匯總。1、android fragment中onActivityResul