Monday, March 3, 2014

How to set the brightness of the Android system



Source code is to use IPowerManager

private void setBrightness (int brightness) {
try {
IPowerManager power = IPowerManager.Stub.asInterface (
ServiceManager.getService ("power"));
if (power! = null) {
power.setBacklightBrightness (brightness);
}
} Catch (RemoteException doe) {

}
}

However, this method requires a signature A system. . . .
There is no other way, or Si Lua?
I see Android optimized master to achieve this function?
I hope the great God to help Afghanistan.<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Some time ago I also can adjust
tangled with this issue, but now they can not be considered completely solved, only activity when activatedBasically online content, do not mind you look at it
 
package com.piaohong.sensor;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.PowerManager;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.util.Log;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

public class TestSensorActivity extends Activity {
/ / Set LOG label
private static final String LOG_TAG = "sensor";
private SensorManager sm;

TextView TV_Info;
Activity MyActivity;
/ ** Called when the activity is first created. * /
@ Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.main);

TV_Info = (TextView) findViewById (R.id.TV_Info);
MyActivity = this;

/ / Create a system of sensors to obtain SensorManager service
sm = (SensorManager) getSystemService (Context.SENSOR_SERVICE);
/ / Select the acceleration sensor
int sensorType = Sensor.TYPE_LIGHT;

/ *
* One of the most commonly used method to register event
* Parameter 1: SensorEventListener listener
* Parameter 2: Sensor a service may have multiple Sensor to achieve here call getDefaultSensor get the default Sensor
* Parameter 3: Mode selectable refresh rate
data changes** /
sm.registerListener (myAccelerometerListener, sm.getDefaultSensor (sensorType), SensorManager.SENSOR_DELAY_NORMAL);

}

Screen brightness / / Android 2.1 + seems to provide automatic time adjustment function, so if the automatic adjustment function when it is turned on, we did not seem to adjust a little role, which makes me very silent, the results only judge to see if the screen brightness turned the automatic adjustment feature.

/ **
* Determine whether to open an automatic brightness adjustment
*
* @ Param aContext
* @ Return
* /
public static boolean isAutoBrightness (ContentResolver aContentResolver) {
boolean automicBrightness = false;
try {
automicBrightness = Settings.System.getInt (aContentResolver,
Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
} Catch (SettingNotFoundException e) {
e.printStackTrace ();
}
return automicBrightness;
}

/ / Then is to find the current brightness, this is more tangled:

/ **
* Get the screen brightness
*
* @ Param activity
* @ Return
* /
public static int getScreenBrightness (Activity activity) {
int nowBrightnessValue = 0;
ContentResolver resolver = activity.getContentResolver ();
try {
nowBrightnessValue = android.provider.Settings.System.getInt (
resolver, Settings.System.SCREEN_BRIGHTNESS);
} Catch (Exception e) {
e.printStackTrace ();
}
return nowBrightnessValue;
}

/ / How does it modify the brightness of the screen?

/ **
* Set the brightness
*
* @ Param activity
* @ Param brightness
* /
public static void setBrightness (Activity activity, int brightness) {
WindowManager.LayoutParams lp = activity.getWindow () getAttributes ();.
lp.screenBrightness = Float.valueOf (brightness) * (1f / 255f);
activity.getWindow () setAttributes (lp);.

}

/ / So, to set up, but why still appear, set up, did not reflect it?

/ / Hey, it is because, opened the automatic adjustment function, and how that close? This is the most important:

/ **
* Stop Automatic brightness adjustment
*
* @ Param activity
* /
public static void stopAutoBrightness (Activity activity) {
Settings.System.putInt (activity.getContentResolver (),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}

/ / Can open, that nature should be able to shut down yo yo, then how close? Very simple:

/ **
* Open the automatic brightness adjustment
*
* @ Param activity
* /
public static void startAutoBrightness (Activity activity) {
Settings.System.putInt (activity.getContentResolver (),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
}

/ / At this point, it should be said that almost every operating brightness, the end!

/ / Hey, would think it should be over, but the tragedy was that, since, as just like setting, it can only have a role in the current activity, the period from the time, you will find no effect, tragedy, turned out to be forgotten saved. Khan!

/ **
* Save the brightness setting status
*
* @ Param resolver
* @ Param brightness
* /
public static void saveBrightness (ContentResolver resolver, int brightness) {
Uri uri = android.provider.Settings.System
. GetUriFor ("screen_brightness");
android.provider.Settings.System.putInt (resolver, "screen_brightness",
brightness);
/ / Resolver.registerContentObserver (uri, true, myContentObserver);
resolver.notifyChange (uri, null);
}

/ / YY drops, which is almost back to the bar, sweat!





/ *
* Achieve SensorEventListener interface, you need to implement two methods
* Method 1 onSensorChanged when data changes are triggered when calling

* When the method is called when 2 onAccuracyChanged accuracy of the obtained data changes, such as suddenly unable to obtain data** /
final SensorEventListener myAccelerometerListener = new SensorEventListener () {

/ / Replication onSensorChanged method
public void onSensorChanged (SensorEvent sensorEvent) {
if (sensorEvent.sensor.getType () == Sensor.TYPE_LIGHT) {
Log.i (LOG_TAG, "onSensorChanged");

/ / Illustration
already explained the meaning of the three valuesfloat X_lateral = sensorEvent.values ​​[0];
Log.i (LOG_TAG, "\ n heading" + X_lateral);
int Lightness;

if (X_lateral <11)
Lightness = (1);
else if (X_lateral <46)
Lightness = (20);
else if (X_lateral <96)
Lightness = (40);
else if (X_lateral <201)
Lightness = (52);
else if (X_lateral <401)
Lightness = (64);
else if (X_lateral <551)
Lightness = (84);
else if (X_lateral <901)
Lightness = (94);
else if (X_lateral <1201)
Lightness = (110);
else if (X_lateral <2001)
Lightness = (130);
else if (X_lateral <3001)
Lightness = (168);
else
Lightness = (255);

setBrightness (MyActivity, Lightness);
saveBrightness (getContentResolver (), Lightness);

TV_Info.setText (
String.valueOf (X_lateral) + "\ n" +
String.valueOf (Lightness) + "\ n" +
);
}
}
/ / Replication onAccuracyChanged method
public void onAccuracyChanged (Sensor sensor, int accuracy) {
Log.i (LOG_TAG, "onAccuracyChanged");
}
};

public void onPause () {
/ *
* Very critical part: note mentioned in the documentation, even if activity is not visible when the sensor will still continue to work when the test can be found, no normal refresh rate
* Will be very high, so be sure to turn off the trigger onPause method, users consume a lot of power to speak otherwise, very responsible.
** /
sm.unregisterListener (myAccelerometerListener);
super.onPause ();
}

}

Reply:
Some time ago I also can adjust
tangled with this issue, but now they can not be considered completely solved, only activity when activatedBasically online content, do not mind you look at it
 
package com.piaohong.sensor;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.PowerManager;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.util.Log;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

public class TestSensorActivity extends Activity {
/ / Set LOG label
private static final String LOG_TAG = "sensor";
private SensorManager sm;

TextView TV_Info;
Activity MyActivity;
/ ** Called when the activity is first created. * /
@ Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.main);

TV_Info = (TextView) findViewById (R.id.TV_Info);
MyActivity = this;

/ / Create a system of sensors to obtain SensorManager service
sm = (SensorManager) getSystemService (Context.SENSOR_SERVICE);
/ / Select the acceleration sensor
int sensorType = Sensor.TYPE_LIGHT;

/ *
* One of the most commonly used method to register event
* Parameter 1: SensorEventListener listener
* Parameter 2: Sensor a service may have multiple Sensor to achieve here call getDefaultSensor get the default Sensor
* Parameter 3: Mode selectable refresh rate
data changes** /
sm.registerListener (myAccelerometerListener, sm.getDefaultSensor (sensorType), SensorManager.SENSOR_DELAY_NORMAL);

}

Screen brightness / / Android 2.1 + seems to provide automatic time adjustment function, so if the automatic adjustment function when it is turned on, we did not seem to adjust a little role, which makes me very silent, the results only judge to see if the screen brightness turned the automatic adjustment feature.

/ **
* Determine whether to open an automatic brightness adjustment
*
* @ Param aContext
* @ Return
* /
public static boolean isAutoBrightness (ContentResolver aContentResolver) {
boolean automicBrightness = false;
try {
automicBrightness = Settings.System.getInt (aContentResolver,
Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
} Catch (SettingNotFoundException e) {
e.printStackTrace ();
}
return automicBrightness;
}

/ / Then is to find the current brightness, this is more tangled:

/ **
* Get the screen brightness
*
* @ Param activity
* @ Return
* /
public static int getScreenBrightness (Activity activity) {
int nowBrightnessValue = 0;
ContentResolver resolver = activity.getContentResolver ();
try {
nowBrightnessValue = android.provider.Settings.System.getInt (
resolver, Settings.System.SCREEN_BRIGHTNESS);
} Catch (Exception e) {
e.printStackTrace ();
}
return nowBrightnessValue;
}

/ / How does it modify the brightness of the screen?

/ **
* Set the brightness
*
* @ Param activity
* @ Param brightness
* /
public static void setBrightness (Activity activity, int brightness) {
WindowManager.LayoutParams lp = activity.getWindow () getAttributes ();.
lp.screenBrightness = Float.valueOf (brightness) * (1f / 255f);
activity.getWindow () setAttributes (lp);.

}

/ / So, to set up, but why still appear, set up, did not reflect it?

/ / Hey, it is because, opened the automatic adjustment function, and how that close? This is the most important:

/ **
* Stop Automatic brightness adjustment
*
* @ Param activity
* /
public static void stopAutoBrightness (Activity activity) {
Settings.System.putInt (activity.getContentResolver (),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}

/ / Can open, that nature should be able to shut down yo yo, then how close? Very simple:

/ **
* Open the automatic brightness adjustment
*
* @ Param activity
* /
public static void startAutoBrightness (Activity activity) {
Settings.System.putInt (activity.getContentResolver (),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
}

/ / At this point, it should be said that almost every operating brightness, the end!

/ / Hey, would think it should be over, but the tragedy was that, since, as just like setting, it can only have a role in the current activity, the period from the time, you will find no effect, tragedy, turned out to be forgotten saved. Khan!

/ **
* Save the brightness setting status
*
* @ Param resolver
* @ Param brightness
* /
public static void saveBrightness (ContentResolver resolver, int brightness) {
Uri uri = android.provider.Settings.System
. GetUriFor ("screen_brightness");
android.provider.Settings.System.putInt (resolver, "screen_brightness",
brightness);
/ / Resolver.registerContentObserver (uri, true, myContentObserver);
resolver.notifyChange (uri, null);
}

/ / YY drops, which is almost back to the bar, sweat!





/ *
* Achieve SensorEventListener interface, you need to implement two methods
* Method 1 onSensorChanged when data changes are triggered when calling

* When the method is called when 2 onAccuracyChanged accuracy of the obtained data changes, such as suddenly unable to obtain data** /
final SensorEventListener myAccelerometerListener = new SensorEventListener () {

/ / Replication onSensorChanged method
public void onSensorChanged (SensorEvent sensorEvent) {
if (sensorEvent.sensor.getType () == Sensor.TYPE_LIGHT) {
Log.i (LOG_TAG, "onSensorChanged");

/ / Illustration
already explained the meaning of the three valuesfloat X_lateral = sensorEvent.values ​​[0];
Log.i (LOG_TAG, "\ n heading" + X_lateral);
int Lightness;

if (X_lateral <11)
Lightness = (1);
else if (X_lateral <46)
Lightness = (20);
else if (X_lateral <96)
Lightness = (40);
else if (X_lateral <201)
Lightness = (52);
else if (X_lateral <401)
Lightness = (64);
else if (X_lateral <551)
Lightness = (84);
else if (X_lateral <901)
Lightness = (94);
else if (X_lateral <1201)
Lightness = (110);
else if (X_lateral <2001)
Lightness = (130);
else if (X_lateral <3001)
Lightness = (168);
else
Lightness = (255);

setBrightness (MyActivity, Lightness);
saveBrightness (getContentResolver (), Lightness);

TV_Info.setText (
String.valueOf (X_lateral) + "\ n" +
String.valueOf (Lightness) + "\ n" +
);
}
}
/ / Replication onAccuracyChanged method
public void onAccuracyChanged (Sensor sensor, int accuracy) {
Log.i (LOG_TAG, "onAccuracyChanged");
}
};

public void onPause () {
/ *
* Very critical part: note mentioned in the documentation, even if activity is not visible when the sensor will still continue to work when the test can be found, no normal refresh rate
* Will be very high, so be sure to turn off the trigger onPause method, users consume a lot of power to speak otherwise, very responsible.
** /
sm.unregisterListener (myAccelerometerListener);
super.onPause ();
}

}

Reply:
Some time ago I also can adjust
tangled with this issue, but now they can not be considered completely solved, only activity when activatedBasically online content, do not mind you look at it
 
package com.piaohong.sensor;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.PowerManager;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.util.Log;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

public class TestSensorActivity extends Activity {
/ / Set LOG label
private static final String LOG_TAG = "sensor";
private SensorManager sm;

TextView TV_Info;
Activity MyActivity;
/ ** Called when the activity is first created. * /
@ Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.main);

TV_Info = (TextView) findViewById (R.id.TV_Info);
MyActivity = this;

/ / Create a system of sensors to obtain SensorManager service
sm = (SensorManager) getSystemService (Context.SENSOR_SERVICE);
/ / Select the acceleration sensor
int sensorType = Sensor.TYPE_LIGHT;

/ *
* One of the most commonly used method to register event
* Parameter 1: SensorEventListener listener
* Parameter 2: Sensor a service may have multiple Sensor to achieve here call getDefaultSensor get the default Sensor
* Parameter 3: Mode selectable refresh rate
data changes** /
sm.registerListener (myAccelerometerListener, sm.getDefaultSensor (sensorType), SensorManager.SENSOR_DELAY_NORMAL);

}

Screen brightness / / Android 2.1 + seems to provide automatic time adjustment function, so if the automatic adjustment function when it is turned on, we did not seem to adjust a little role, which makes me very silent, the results only judge to see if the screen brightness turned the automatic adjustment feature.

/ **
* Determine whether to open an automatic brightness adjustment
*
* @ Param aContext
* @ Return
* /
public static boolean isAutoBrightness (ContentResolver aContentResolver) {
boolean automicBrightness = false;
try {
automicBrightness = Settings.System.getInt (aContentResolver,
Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
} Catch (SettingNotFoundException e) {
e.printStackTrace ();
}
return automicBrightness;
}

/ / Then is to find the current brightness, this is more tangled:

/ **
* Get the screen brightness
*
* @ Param activity
* @ Return
* /
public static int getScreenBrightness (Activity activity) {
int nowBrightnessValue = 0;
ContentResolver resolver = activity.getContentResolver ();
try {
nowBrightnessValue = android.provider.Settings.System.getInt (
resolver, Settings.System.SCREEN_BRIGHTNESS);
} Catch (Exception e) {
e.printStackTrace ();
}
return nowBrightnessValue;
}

/ / How does it modify the brightness of the screen?

/ **
* Set the brightness
*
* @ Param activity
* @ Param brightness
* /
public static void setBrightness (Activity activity, int brightness) {
WindowManager.LayoutParams lp = activity.getWindow () getAttributes ();.
lp.screenBrightness = Float.valueOf (brightness) * (1f / 255f);
activity.getWindow () setAttributes (lp);.

}

/ / So, to set up, but why still appear, set up, did not reflect it?

/ / Hey, it is because, opened the automatic adjustment function, and how that close? This is the most important:

/ **
* Stop Automatic brightness adjustment
*
* @ Param activity
* /
public static void stopAutoBrightness (Activity activity) {
Settings.System.putInt (activity.getContentResolver (),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}

/ / Can open, that nature should be able to shut down yo yo, then how close? Very simple:

/ **
* Open the automatic brightness adjustment
*
* @ Param activity
* /
public static void startAutoBrightness (Activity activity) {
Settings.System.putInt (activity.getContentResolver (),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
}

/ / At this point, it should be said that almost every operating brightness, the end!

/ / Hey, would think it should be over, but the tragedy was that, since, as just like setting, it can only have a role in the current activity, the period from the time, you will find no effect, tragedy, turned out to be forgotten saved. Khan!

/ **
* Save the brightness setting status
*
* @ Param resolver
* @ Param brightness
* /
public static void saveBrightness (ContentResolver resolver, int brightness) {
Uri uri = android.provider.Settings.System
. GetUriFor ("screen_brightness");
android.provider.Settings.System.putInt (resolver, "screen_brightness",
brightness);
/ / Resolver.registerContentObserver (uri, true, myContentObserver);
resolver.notifyChange (uri, null);
}

/ / YY drops, which is almost back to the bar, sweat!





/ *
* Achieve SensorEventListener interface, you need to implement two methods
* Method 1 onSensorChanged when data changes are triggered when calling

* When the method is called when 2 onAccuracyChanged accuracy of the obtained data changes, such as suddenly unable to obtain data** /
final SensorEventListener myAccelerometerListener = new SensorEventListener () {

/ / Replication onSensorChanged method
public void onSensorChanged (SensorEvent sensorEvent) {
if (sensorEvent.sensor.getType () == Sensor.TYPE_LIGHT) {
Log.i (LOG_TAG, "onSensorChanged");

/ / Illustration
already explained the meaning of the three valuesfloat X_lateral = sensorEvent.values ​​[0];
Log.i (LOG_TAG, "\ n heading" + X_lateral);
int Lightness;

if (X_lateral <11)
Lightness = (1);
else if (X_lateral <46)
Lightness = (20);
else if (X_lateral <96)
Lightness = (40);
else if (X_lateral <201)
Lightness = (52);
else if (X_lateral <401)
Lightness = (64);
else if (X_lateral <551)
Lightness = (84);
else if (X_lateral <901)
Lightness = (94);
else if (X_lateral <1201)
Lightness = (110);
else if (X_lateral <2001)
Lightness = (130);
else if (X_lateral <3001)
Lightness = (168);
else
Lightness = (255);

setBrightness (MyActivity, Lightness);
saveBrightness (getContentResolver (), Lightness);

TV_Info.setText (
String.valueOf (X_lateral) + "\ n" +
String.valueOf (Lightness) + "\ n" +
);
}
}
/ / Replication onAccuracyChanged method
public void onAccuracyChanged (Sensor sensor, int accuracy) {
Log.i (LOG_TAG, "onAccuracyChanged");
}
};

public void onPause () {
/ *
* Very critical part: note mentioned in the documentation, even if activity is not visible when the sensor will still continue to work when the test can be found, no normal refresh rate
* Will be very high, so be sure to turn off the trigger onPause method, users consume a lot of power to speak otherwise, very responsible.
** /
sm.unregisterListener (myAccelerometerListener);
super.onPause ();
}

}

Reply:
/ **
* Set the brightness
*
* @ Param activity
* @ Param brightness
* /
public static void setBrightness (Activity activity, int brightness) {
WindowManager.LayoutParams lp = activity.getWindow () getAttributes ();.
lp.screenBrightness = Float.valueOf (brightness) * (1f / 255f);
activity.getWindow () setAttributes (lp);.

}

This way I have found
Only modify the current brightness is not it. .
I appreciate your source code. .
But I want to modify the system. . .
Egypt really do not know
that android is how to achieve optimization guru
Who can give an idea like the idea of ​​not signing
Reply:
Reference Setting the source of it
Reply:

Source use is IPowerManager ...
This will require a signature to use.

Reply:
http://blog.csdn.net/androidchuxueze/article/details/7452438
I was so used before the project
Reply:
reference to the 7th floor of replies:
http://blog.csdn.net/androidchuxueze/article/details/7452438
Before the project, I was so used


You can also set the brightness of the page currently can not set the brightness of the entire Arab system. . .
Reply:
It is recommended to look at a few of the purview
android.permission.CHANGE_CONFIGURATION
android.permission.WRITE_SETTINGS

Reply:
The lower down the source, then compile your source code on the line
apk

PowerManager manager = (PowerManager) ServiceManager.getService (Context.POWER_SERVICE);
manager.setBacklightBrightness (50);
Reply:
It seems that only in the source code to compile, generate apk, you do also to others with the apk, may be relatively large
Reply:
Landlord, Will finally found a solution yet?
Set in the brightness of the system, if it is to manually adjust the brightness mode, slide, brightness can be changed in real time. When applied in the brightness of the slide, the brightness has been the same ......
Reply:
Finally, my solution is: http://coldsummerwei.iteye.com/admin/blogs/1964174 < / a>

No comments:

Post a Comment