2013-07-30 33 views
4

我想模仿可以控制設備亮度的Andorid設置小部件。它按預期工作,我可以看到在設置中看到。但我沒有看到設備變亮。亮度控制不反映在設備上

下面是代碼:

WindowManager.LayoutParams lp = getWindow().getAttributes(); 
    SeekBar brightnessControl  = (SeekBar) findViewById(R.id.sbBrightness); 
    int currentBrightness   = 10; 

    brightnessControl.setProgress(currentBrightness); 
    lp.screenBrightness = currentBrightness/100f; 

    brightnessControl.setOnSeekBarChangeListener(new OnSeekBarChangeListener() 
    { 

     @Override 
     public void onStopTrackingTouch(SeekBar seekBar) {} 

     @Override 
     public void onStartTrackingTouch(SeekBar seekBar) {} 

     @Override 
     public void onProgressChanged(SeekBar seekBar, int progress, 
       boolean fromUser) 
     { 
      //set local brightness 
      WindowManager.LayoutParams lp = getWindow().getAttributes(); 
      progress      = (progress <= 10)?10:progress; 
      lp.screenBrightness    = progress/100f; 
      getWindow().setAttributes(lp); 

      //put local to system wide brightness 
      int sysWideBrightness   = (int) (progress/100f * 255); 
      android.provider.Settings.System.putInt(
        getContentResolver(), 
        android.provider.Settings.System.SCREEN_BRIGHTNESS, 
        sysWideBrightness); 
     } 
    }); 

我會提到,本次活動是活動選項卡上發射。在將它放入標籤之前,它可以正常工作,但並非如此。

我注意到,當我把我的設備上的新版本的android(現在4.2.2根),它能夠按預期工作。我在一個有根的薑餅設備上測試過它不起作用。

這可能有幫助,這是Main.java文件,以下是ActivityTab的啓動方式。

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    /* TabHost will have Tabs */ 
    tabHost = (TabHost)findViewById(android.R.id.tabhost); 

    TabSpec firstTabSpec = tabHost.newTabSpec("tid1"); 
    TabSpec secondTabSpec = tabHost.newTabSpec("tid2"); 
    TabSpec thirdTabSpec = tabHost.newTabSpec("tid3"); 
    TabSpec fourthTabSpec = tabHost.newTabSpec("tid4"); 
    TabSpec fifthTabSpec = tabHost.newTabSpec("tid5"); 

    firstTabSpec.setIndicator("", getResources().getDrawable(R.drawable.clear_cache_tab)).setContent(showClearCache()); 
    secondTabSpec.setIndicator("", getResources().getDrawable(R.drawable.move_sd_tab)).setContent(showMoveToSd()); 
    thirdTabSpec.setIndicator("", getResources().getDrawable(R.drawable.remove_app_tab)).setContent(showRemoveApp()); 
    fourthTabSpec.setIndicator("", getResources().getDrawable(R.drawable.feature_manager_tab)).setContent(showFeatureManager()); 
    fifthTabSpec.setIndicator("", getResources().getDrawable(R.drawable.feature_manager_tab)).setContent(showProcessKill()); 

    tabHost.setOnTabChangedListener(this); 
    tabHost.addTab(firstTabSpec); 
    tabHost.addTab(secondTabSpec); 
    tabHost.addTab(thirdTabSpec); 
    tabHost.addTab(fourthTabSpec); 
    tabHost.addTab(fifthTabSpec); 

    for(int i=0;i<tabHost.getTabWidget().getChildCount();i++) 
    { 
     tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#303030")); 
    } 

    tabHost.getTabWidget().setCurrentTab(0); 
    tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#C8C8C8")); 
} 

@Override 
public void onTabChanged(String tabId) { 
    // TODO Auto-generated method stub 
    for(int i=0;i<tabHost.getTabWidget().getChildCount();i++) 
    { 
     tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#303030")); 
    } 

    tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#C8C8C8")); 
} 

回答

0

你不刷新屏幕。所以亮度不會改變。

Settings.System.putInt(this.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 20); 

    WindowManager.LayoutParams lp = getWindow().getAttributes(); 
    lp.screenBrightness =0.2f;// 100/100.0f; 
    getWindow().setAttributes(lp); 

    startActivity(new Intent(this,RefreshScreen.class)); 

創建一個名爲RefreashScreen的虛擬活動,它只是調用完成。 20是亮度級別的整數,因此可以用你想要的來代替它。我希望這有幫助。

+0

對不起,我將代碼更改爲Seekbar。但我不認爲我可以使用它開始一項活動。有什麼建議麼? –

+0

但我沒有檢查舊腳本,它仍然無法在HoneyComb上工作。 –

0

NightSky的答案几乎是正確的,但我想我知道它爲什麼不適合你。事實上,你必須製作一個透明的「虛擬」活動來處理亮度變化並刷新它。我不知道這將如何與SeekBar一起工作,但它是我能想到的唯一方法就是刷新窗口。

將在OnCreate下面的代碼()透明性(不會在當前活動,但在虛擬的)的: -

WindowManager.LayoutParams lp = getWindow().getAttributes(); 
lp.screenBrightness = brightness; //Use your brightness value or an arbitrary one 
getWindow().setAttributes(lp); 

/*Here is the real difference. You need to allow some time for you screen to be 
refreshed before you call finish() on your activity. This means we should make a 
thread that calls finish() after a small amount of time, say 300ms (You can use a 
different value if you want). */ 

new Handler().postDelayed(new Runnable() { 

    @Override 
    public void run() { 
     finish(); 
    } 

}, 300); 

這在過去爲我工作。正如我之前提到的,我不認爲有一種方法可以讓你使用SeekBar,它會在你改變它的值時不斷改變亮度。你必須刷新屏幕。

+0

謝謝。我稍後再檢查一下。 –

0

下面是我工作的代碼(作爲一個widget):

基本上,啓動做調諧活動。

BrightnessActivity.java:

package com.xxx.switchwidget; 

import com.xxx.Utils; 

import android.app.Activity; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.view.WindowManager; 

public class BrightnessActivity extends Activity { 

    private static final boolean DEBUG = true; 

    private static final String TAG = "BrightnessActivity"; 

    private Handler mHandler = new Handler() { 
     public void handleMessage(Message msg) { 
      finish(); 
     } 
    }; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     int brightness = SwitchWidget.getNextBrightness(this); 
     if (brightness < 0) { 
      finish(); 
      return; 
     } 
     WindowManager.LayoutParams params = getWindow().getAttributes(); 
     params.screenBrightness = Float.valueOf(brightness/Float.valueOf(SwitchWidget.MAXIMUM_BACKLIGHT)).floatValue(); 
     getWindow().setAttributes(params); 

     toggleBrightness(); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     if (DEBUG) 
      Utils.log(TAG, "onDestroy()"); 

    } 

    private void toggleBrightness() { 
     new AsyncTask<Void, Integer, Void>() { 
      @Override 
      protected Void doInBackground(Void... args) { 
       SwitchWidget.toggleBrightness(BrightnessActivity.this);//TODO: Change to your own Activity 
       publishProgress(1); 
       return null; 
      } 

      @Override 
      protected void onProgressUpdate(Integer... args) { 
       if (DEBUG) 
        Utils.log(TAG, "onProgressUpdate"); 
       mHandler.sendEmptyMessage(0); 
      } 

      @Override 
      protected void onPostExecute(Void result) { 
      } 
     }.execute(); 
    } 

} 

而在你的活動(Tab)的方法:(這是* SwitchWidget在我的代碼) *

public static void toggleBrightness(Context context) { 
     try { 
     ContentResolver cr = context.getContentResolver(); 
     int brightness = Settings.System.getInt(cr, 
       Settings.System.SCREEN_BRIGHTNESS); 
     boolean isBrightnessModeChanged = false; 
     int brightnessMode = Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL; 

     brightnessMode = Settings.System.getInt(cr, 
       Settings.System.SCREEN_BRIGHTNESS_MODE); 

     // Rotate AUTO -> MINIMUM -> DEFAULT -> MAXIMUM 
     // Technically, not a toggle... 
     if (brightnessMode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) { 
      brightness = DEFAULT_BACKLIGHT; 
      brightnessMode = Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL; 
      isBrightnessModeChanged = true; 
     } else if (brightness < DEFAULT_BACKLIGHT) { 
      brightness = DEFAULT_BACKLIGHT; 
     } else if (brightness < MAXIMUM_BACKLIGHT) { 
      brightness = MAXIMUM_BACKLIGHT; 
     } else { 
      brightness = MINIMUM_BACKLIGHT; 
     } 

     // Set screen brightness mode (automatic or manual) 
     if (isBrightnessModeChanged) { 
      Settings.System.putInt(context.getContentResolver(), 
        Settings.System.SCREEN_BRIGHTNESS_MODE, brightnessMode); 
     } 

     if (brightnessMode == Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL) { 
      Settings.System.putInt(cr, Settings.System.SCREEN_BRIGHTNESS, 
        brightness); 
     } 

     } catch (Settings.SettingNotFoundException e) { 
      if (DEBUG) 
       Utils.log(TAG, "toggleBrightness: " + e); 
     } 
    } 

最後調用這設置亮度:

Intent newIntent = new Intent(mContext, BrightnessActivity.class); 
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
mContext.startActivity(newIntent); 

希望這可能會有所幫助。