Service中調節屏幕亮度(不依賴Activity)的實現

      項目中需要做一個桌面小工具,會懸浮在桌面(類似360等的清理內存懸浮框),點擊後又手電筒、wifi、屏幕亮度等開關,因爲需要在app退出後也能夠運行,所以需要由Service實現。

其中的設置屏幕亮度的方法如下:

Settings.System.putInt(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightness);


如果當開啓自動調節功能的時候,我們進行調節好像是沒有一點作用的,只有進行判斷,看是否開啓了屏幕亮度的自動調節功能。

    /**
     *
     * @return 1:表示是自動亮度
     */

    private int getScreenMode(){
        int screenMode=0;
        try{
            screenMode = Settings.System.getInt(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE);
        }
        catch (Exception localException){
        }
        return screenMode;
    }

關閉自動亮度

    private void stopAutoBrightness(){
        Settings.System.putInt(mContext.getContentResolver(),
                Settings.System.SCREEN_BRIGHTNESS_MODE,
                Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
    }


至此,應該說操作亮度的基本都差不多了,在4.4的虛擬機和小米手機上運行正常。

但是悲劇的是,測試提出bug,在三星的手機上調節亮度無效,於是拿機器過來一看是4.1.2,查了相關知識才知道,在Android低版本調用putInt後需要刷新一下才能使調節

屏幕亮度生效,鎖屏一下再次打開,果然生效了。。但是不可能讓用戶每次來修改亮度時還需要再鎖屏一下吧。

WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness=brightness;//設置屏幕的亮度
getWindow().setAttributes(layoutParams);

上面的方法也有效,但是getWindow只能在Activity中調用,而我們是Service,於是這個方法也行不通。

還有其他的方法

Uri uri = android.provider.Settings.System
            .getUriFor("screen_brightness");

mContext.getContentResolver().notifyChange(uri, null);

經過測試也是無效的,還是不能立刻刷新當前的屏幕亮度。

沒辦法只能繼續找答案了。

是不是可以打開一個透明的Activity,然後利用它的Window來設置屏幕亮度完了後結束它,這樣也不行,打開一個Activity時也會啓動我們的app,這樣不是用Service來實現的。


由於我們都是使用WindowManager在桌面創建的View,查了下WindowManager,發現它的LayoutParams有一個screenBrightness的屬性,這個屬性應該能改變桌面的亮度。應該可以用WindowManager創建一個全屏的透明View,在addView時設置LayoutParams的screenBrightness爲修改的亮度值brightness。然後再刪除這個view。

    private static void setScreenBrightness(int brightness){
        if(mWindowManager == null){
            mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        }
  
	    int screenWidth = mWindowManager.getDefaultDisplay().getWidth();
        int screenHeight = mWindowManager.getDefaultDisplay().getHeight();
        if (mScreenLayout == null) {
            mScreenLayout = new LinearLayout(mContext);
            if (mScreenLayoutParmas == null) {
                mScreenLayoutParmas = new WindowManager.LayoutParams();
                mScreenLayoutParmas.x = 0;
                mScreenLayoutParmas.y = 0;
                mScreenLayoutParmas.type = WindowManager.LayoutParams.TYPE_PHONE;
                mScreenLayoutParmas.format = PixelFormat.RGBA_8888;
                mScreenLayoutParmas.width = screenWidth;
                mScreenLayoutParmas.height = screenHeight;
		//brightness 是Settings.System.putInt()中對應的int,轉換成WindowManager.LayoutParams中的float
                mScreenLayoutParmas.screenBrightness = brightness / 255.0f;
            }

	    mWindowManager.addView(mScreenLayout, mScreenLayoutParmas);
            mWindowManager.removeView(mScreenLayout);
            mScreenLayout = null;
            mScreenLayoutParmas = null;
        }
    }


需要權限<uses-permission android:name="android.permission.WRITE_SETTINGS" />。
在三星手機(4.1.2)上測試正常,真的能修改屏幕的亮度了。。好神奇。。於是趕緊記錄下來。





發佈了35 篇原創文章 · 獲贊 4 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章