Brightness control does not affect the device

I am trying to simulate an Andorid settings widget that can control the brightness of a device. It works as intended, and I see it in the settings. But I donโ€™t see the device shining.

Here is the code:

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

I would mention that this action runs on the Activity tab. Before putting it on a tab, it works fine, but it is not.

I notice that when I installed the new version of android on my device (now 4.2.2), it was able to work as intended. I tested it on the root device of gingerbread, this did not work.

This may help, this is the Main.java file, and this is how the ActivityTab is triggered.

 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
source share
3 answers

You are not updating the screen. Thus, the brightness will not change.

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

Create a dummy activity called RefreashScreen that just triggers the finish. 20 is the brightness level, so replace it with whatever you want. Hope this helps.

0
source

The answer to NightSky is almost correct, but I think I know why it does not work for you. You really need to make the transparent activity "dummy", which handles the change in brightness and updates it. I'm not sure how this will work with SeekBar, but this is the only way I can think that it will refresh the window.

Put the following code in onCreate () of transparent activity (not in current activity, but in fictitious): -

 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, I donโ€™t think there is a method that allows you to use a SeekBar that constantly changes brightness when its value changes. You must refresh the screen.

0
source

The following is my working code (as a widget):

Basically, it starts an operation that performs the setup.

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

And the method in your activity (tab): (this is my * SwitchWidget in my code) *

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

Finally, call this to set the brightness:

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

Wish it helps.

0
source

Source: https://habr.com/ru/post/1494209/


All Articles