Amazon Gift Cards

Monday, January 5, 2009

Changing the Screen Brightness Programatically

Update: If your target is cupcake(android 1.5), you might want to see this tutorial

There are some discussion on Google group on this matter so i try to see if how this problem can be fixed. This problem could be fixed by using something that is not documented and is not advised to use (Its sort of a hack, i'm not responsible if something went wrong on your android). What this app will do it count to 240, 20 per second starting from 0. Then on each count it will set the brightness of your phone.

Anyway here is the how i did it. Please read the Note at the bottom.

First add <uses-permission android:name="android.permission.HARDWARE_TEST"></uses-permission> to your Manifest file.
package com.monmonja.firstDemo;

import android.os.Bundle;
import android.os.Handler;
import android.os.IHardwareService;
import android.os.Message;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.widget.TextView;

public class Main extends Activity  { 
  private TextView txtStatus;
  private RefreshHandler mRedrawHandler = new RefreshHandler();

  class RefreshHandler extends Handler {
    public void handleMessage(Message msg) {

    public void sleep(long delayMillis) {
      sendMessageDelayed(obtainMessage(0), delayMillis);

  private void updateUI(){
    int currentInt = Integer.parseInt((String) txtStatus.getText()) + 20;
    if(currentInt <= 250){

  public void onCreate(Bundle icicle) { 
    this.txtStatus = (TextView) this.findViewById(;

  private void setBrightness(int brightness) {
    try {
      IHardwareService hardware = IHardwareService.Stub.asInterface(
      if (hardware != null) {
    } catch (RemoteException doe) {          

By default there is no IHardwareService and other classes related to it. In order to have this you can download hardware09.jar and place it on your application folder (In my app, i created a new folder named lib). In order to use this on eclipse, you need to right click on your application, Properties, Java Build Paths, and click Add JAR.

Set to include hardware.jar

Quick Explanation
We get our hardware services via IHardwareService hardware = IHardwareService.Stub.asInterface(ServiceManager.getService("hardware")); then we set the brightness hardware.setScreenBacklight(brightness);

Source Code
Main.xml of Changing Brightness of Changing Brightness

Using Handler in Android
FlashLight Source File

Hope you will get the hardware09.jar to work/referenced in your eclipse.

Hope it helps :)

Update History
   Jan 17, 2012 - Visual Update
Post a Comment