Toby
Toby

Reputation: 151

Android Activity to test Backlight

I am trying to make a simple blank app that tests the backlight of the phone. The activity works, it turns the brightness up to 255. However the app never shuts down, when I add onDestroy() at the end it crashes. It also throws exceptions anywhere the Settings.System... is called but does not crash. I would also like to be able to reset it to default brightness after the apps runs. I have been playing with this simple app for so long and cant get it right, please help!!!

public class BacklightActivity extends Activity {


    int brightness =  255;
    public final static String log_tag = "Backlight";
    private Handler mHandler = new Handler();
    int delay = 10000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.i(log_tag,"Entered onCreate()");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_backlight);
        /*
         * API's to launch the application when the tablet is locked or
         * display is turned off
         */
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        createTempFile("Status_Backlight.txt", "INPROGRESS");
        try {
            //this will set the manual mode (set the automatic mode off)
            Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
            //Sets the bightness of the backlight (1-255)  britness is set at max of 255
            Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightness);  //this will set the brightness to maximum (255)

            //refreshes the screen
            int br = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.screenBrightness = (float) br / 255;
            getWindow().setAttributes(lp);

        } catch (Exception e) {
        }
        //Delay turing off flash and then end activity
        mHandler.postDelayed(new Runnable() {
            public void run() {
                Log.i(log_tag,"Entered sleep");
            }
        }, delay);
//        Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);

    }

    private void exit_function(){
        onDestroy();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(log_tag,"Entered onDestroy()");
        createTempFile("Status_Backlight.txt", "COMPLETED");
        Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
        finish();

        }

Upvotes: 1

Views: 430

Answers (1)

Amir_P
Amir_P

Reputation: 9019

for closing application just use

this.finish();

and for Settings.System you need WRITE_SETTINGS permission. replace these lines from your activity onCreate

    try {
        //this will set the manual mode (set the automatic mode off)
        Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
        //Sets the bightness of the backlight (1-255)  britness is set at max of 255
        Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightness);  //this will set the brightness to maximum (255)

        //refreshes the screen
        int br = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.screenBrightness = (float) br / 255;
        getWindow().setAttributes(lp);

    } catch (Exception e) {
    }

with these lines

if (!android.provider.Settings.System.canWrite(this)) {
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_SETTINGS}, WRITE_PERMISSION_REQUEST);
} else {
    systemsetting();
}

and add this code to your activity

@Override
  public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
      case WRITE_PERMISSION_REQUEST:
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
          systemsetting();
        } else {
          finish();
        }
      default:
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
  }
  public void systemsetting(){
        try {
            //this will set the manual mode (set the automatic mode off)
            Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
            //Sets the bightness of the backlight (1-255)  britness is set at max of 255
            Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightness);  //this will set the brightness to maximum (255)

            //refreshes the screen
            int br = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.screenBrightness = (float) br / 255;
            getWindow().setAttributes(lp);

        } catch (Exception e) {
        }
  }

Upvotes: 2

Related Questions