用于测试背光的Android Activity

时间:2016-11-17 19:45:24

标签: android android-studio android-settings

我正在尝试制作一个简单的空白应用程序来测试手机的背光。活动有效,它将亮度调高到255.但是应用程序永远不会关闭,当我最后添加onDestroy()时崩溃。它还会在调用Settings.System ...的任何地方抛出异常,但不会崩溃。我还希望能够在应用程序运行后将其重置为默认亮度。我一直在玩这个简单的应用程序这么久,不能正确,请帮助!

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();

        }

1 个答案:

答案 0 :(得分:2)

关闭申请只需使用

this.finish();

Settings.System您需要WRITE_SETTINGS权限。从您的活动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) {
    }

这些行

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

并将此代码添加到您的活动中

@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) {
        }
  }