android 4.2如何调整屏幕亮度

时间:2014-07-17 15:06:56

标签: java android

我有一个Android设备,它只能运行一个应用程序(如ATM屏幕)。
现在我想实现一个功能: 如果设备未使用超过30分钟,我会将屏幕亮度调整到最低。此时,如果我触摸屏幕,我应该将屏幕亮度调整到最大。用户看不到任何android系统菜单,app等,他们只能使用这个应用程序(不能关闭它) 这个应用程序将在此设备上运行它的电源并关闭它。 我不知道如何实现这个功能。 感谢。

2 个答案:

答案 0 :(得分:1)

您可以使用扩展service的类来调暗屏幕亮度,并使用AlarmManager检查用户从不触摸屏幕的时间,我将举例说明如何使用service

public class DimScreen extends Service {

public static  int ID_NOTIFICATION = 2018;

private WindowManager windowManager;
private LinearLayout saverScreen;
private PopupWindow pwindo;

boolean mHasDoubleClicked = false;
long lastPressTime;
private Boolean _enable = true;


@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override 
public void onCreate() {
    super.onCreate();
    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    saverScreen = new LinearLayout(this);
    saverScreen.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
    Bitmap sample = Bitmap.createBitmap(100, 100, Config.ARGB_8888);
    saverScreen.setBackground(new BitmapDrawable(this.getResources(),
            convertColorIntoBlackAndWhiteImage(sample)));
    saverScreen.setClickable(false);
    saverScreen.setFocusable(false);
    final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.FILL_PARENT,
            WindowManager.LayoutParams.FILL_PARENT,
            PixelFormat.TRANSLUCENT);
    params.flags = WindowManager.LayoutParams.FLAG_FULLSCREEN
            |WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
            |WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
            |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
            |WindowManager.LayoutParams.FLAG_DIM_BEHIND;
    params.dimAmount = (float) 0.6;
    params.screenBrightness = (float) 0.3;
    params.systemUiVisibility = View.SYSTEM_UI_FLAG_LOW_PROFILE;
    windowManager.addView(saverScreen, params);
}
@Override
public void onDestroy() {
    super.onDestroy();
    if (saverScreen != null) windowManager.removeView(saverScreen);
}
private Bitmap convertColorIntoBlackAndWhiteImage(Bitmap orginalBitmap) {
    ColorMatrix colorMatrix = new ColorMatrix();
    colorMatrix.setSaturation(2);
    ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter(
            colorMatrix);

    Bitmap blackAndWhiteBitmap = orginalBitmap.copy(
            Bitmap.Config.ARGB_8888, true);

    Paint paint = new Paint();
    paint.setColorFilter(colorMatrixFilter);

    Canvas canvas = new Canvas(blackAndWhiteBitmap);
    canvas.drawBitmap(blackAndWhiteBitmap, 0, 0, paint);

    return blackAndWhiteBitmap;
}

}

在您的Activity类调用

startService(new Intent(this,DimScreen.class));

如果用户从不触摸屏幕,您只需要立即实施AlarmManager启动Service课程,如果用户使用应用中断,则stopService

答案 1 :(得分:1)

试试这个

WindowManager.LayoutParams localLayoutParams = getWindow() .getAttributes(); localLayoutParams.screenBrightness = 0.12F; getWindow().setAttributes(localLayoutParams);