How to change screen brightness in BroadcastReceiver?

I want to change the brightness, I can use this method:

public static void SetBright(int brightness, Context context) { if (isAutoBrightness(context)) { stopAutoBrightness(context); } WindowManager.LayoutParams lp = ((Activity) context).getWindow() .getAttributes(); lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f); ((Activity) context).getWindow().setAttributes(lp); } 

I need an operation to go to SetBright(int brightness, Context context);

But now I need to call the SetBright(int brightness, Context context) method in Brocastreceiver. I can use the context in the onReceive method (context context, intent intention), but if I exit the application, it does not work.

Is there any other way that I can use to change brightness instead of using activity?

+4
source share
3 answers

start fictitious activity and set the window parameter using brightness (from 0 to 1 range-1 for 255). start the timer 50, 100 or 500 ms independently. then complete the work.

 TimerTask finishTask = new TimerTask() { @Override public void run() { BrightActivity.this.finish(); timer.cancel(); if (timer != null) { timer = null; } } }; timer.schedule(finishTask,Constants.BRIGHTNESS_REFRESH_DELAY); 
+4
source

First of all, I used 500 ms for the screen refresh time, but it does not work in the s3 galaxy. but 1000 is fine. Thus, the screen refresh time is device dependent.

+1
source

if you want to save, try this after adjusting the brightness:

 public static void saveBrightness(Context context, int brightness) { ContentResolver resolver= context.getContentResolver(); Uri uri = android.provider.Settings.System .getUriFor("screen_brightness"); android.provider.Settings.System.putInt(resolver, "screen_brightness", brightness); resolver.notifyChange(uri, null); } 
0
source

Source: https://habr.com/ru/post/1413542/


All Articles