使用IHardwareService
接口這樣的:
許可
<uses-permission android:name="android.permission.HARDWARE_TEST"></uses-permission>
修改代碼,如:
private void setBrightness(int brightness) {
try {
IHardwareService hardware = IHardwareService.Stub.asInterface(
ServiceManager.getService("hardware"));
if (hardware != null) {
hardware.setScreenBacklight(brightness);
}
} catch (RemoteException doe) {
}
完整的例子這裏Changing the Screen Brightness
或在您的情況下,也許可能:
WindowManager.LayoutParams lp = getWindow().getAttributes();
移動這個給你做setContentView(R.layout.main);
後構造窗口之前,你不能這樣做getWindow().getAttributes()
。
因此,你的代碼將成爲
public class MainActivity extends Activity {
/** Called when the activity is first created. */
// MY BRIGHTNESS VARIABLES
WindowManager.LayoutParams lp;
float fb;
float lb = 0;
float hb = 1;
//////////////////////////////////////////////////////////////////////////
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lp = getWindow().getAttributes();
fb = lp.screenBrightness;
// MY CODE FROM HERE DOWN
Button button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(lp.screenBrightness==fb) {
lp.screenBrightness=lb;
getWindow().setAttributes(lp);
}
if(lp.screenBrightness==lb){
lp.screenBrightness=hb;
getWindow().setAttributes(lp);
}
if(lp.screenBrightness==hb){
lp.screenBrightness=fb;
getWindow().setAttributes(lp);
}
}
});
//////////////////////////////////////////////
}
}
此代碼完成的活動,同時調用相同的代碼由於activity.finish()語句,當我刪除了這一說法那麼這段代碼執行但屏幕上沒有任何影響。 – 2012-03-14 06:03:22
欲瞭解更多信息看[autobright](http://code.google.com/p/autobright/source/browse/trunk/autobright/src/com/geekyouup/android/autobright/AutoBright.java) – user370305 2012-03-14 06:03:27
嘿Thanx它通過將divident 255.0f增加到1500.0f來工作。 – 2012-03-14 06:19:41