简体   繁体   中英

android 4.2 how to adjust the screen brightness

I have an android device, it can only run a app (like ATM screen). Now I want to implement the following feature:

If the device is not in use for over 30 minutes, I will adjust the screen brightness to the lowest. At this time, if I touch the screen, I should adjust the screen brightness to the maximum. The user can not see any Android system menu, application, etc. They only can use this app (can't close it). This app will run in this device from the power on it and power off it.

I don't how to implement this feature.
Thanks.

You can use a class that extends service and can dim the screen brightness. Use AlarmManager to check the time that the user never touches the screen. I will give you an example of using the Service class:

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;
    }
}

In your Activity class call

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

You only have to implement the AlarmManager now. If the user never touches the screen, launch the Service class. If the user Interrupt with the app, then call stopService .

尝试这个

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM