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"));
}
4

3 回答 3

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 是亮度级别的整数,所以用你想要的替换它。我希望这有帮助。

于 2013-08-02T07:02:05.020 回答
0

以下是我的工作代码(作为小部件):

基本上,它会启动一个进行调整的 Activity。

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();
    }

}

以及您的 Activity(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);

希望这可能会有所帮助。

于 2013-08-09T01:15:06.630 回答
0

NightSky's answer is almost correct but I think I know why it's not working for you. You will, indeed, have to make a transparent "dummy" activity that processes the brightness change and refreshes it. I'm not sure how this will work with a SeekBar but it is the only way I can think of that will refresh the Window.

Place the following code in the onCreate() of the transparent activity (not in the current activity, but in the dummy one):-

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);

This has worked for me in the past. As I mentioned earlier though, I don't think there is a method that will allow you to use a SeekBar which continually changes brightness as you change it's value. You have to refresh the screen.

于 2013-08-07T11:17:06.540 回答