Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>the following is my working code (As a widget):</p> <p>Basically, it start a Activity that do the tuning.</p> <p>BrightnessActivity.java:</p> <pre><code>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 &lt; 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&lt;Void, Integer, Void&gt;() { @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(); } } </code></pre> <p><strong>And the method in your Activity(Tab): (It is *<em>SwitchWidget</strong> in my code)</em>*</p> <pre><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 -&gt; MINIMUM -&gt; DEFAULT -&gt; 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 &lt; DEFAULT_BACKLIGHT) { brightness = DEFAULT_BACKLIGHT; } else if (brightness &lt; 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); } } </code></pre> <p>Finally invoke this to set brightness:</p> <pre><code>Intent newIntent = new Intent(mContext, BrightnessActivity.class); newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); mContext.startActivity(newIntent); </code></pre> <p>Wish this might help.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload