android change brightness setting from BroadcastReceiver -
i have app installed on system partition (android 4.4.1 , 4.1.2). need set brightness of screen according battery level. so, register on application
class broadcastreceiver
listens intent.action_battery_changed
intent check battery status.
i've seen many answers how on activity
, on service
using fake activity
. use service
- activity
approach don't want start activity apply brightness setting windowmanager
.
this have:
contentresolver contentresolver = context.getcontentresolver(); int currentbrightness = settings.system.getint(context.getcontentresolver(), settings.system.screen_brightness, -1); int newbrightness = -1; // set brightness according battery level if (batterypercentage > batteryutils.battery_safe_level && currentbrightness != brightness_high) { newbrightness = brightness_high; } else if (batterypercentage <= batteryutils.battery_safe_level && batterypercentage > batteryutils.battery_low_level && currentbrightness != brightness_normal) { newbrightness = brightness_normal; } else if (batterypercentage <= batteryutils.battery_low_level && currentbrightness != brightness_low) { newbrightness = brightness_low; } if (newbrightness != -1) { settings.system.putint(contentresolver, settings.system.screen_brightness_mode, settings.system.screen_brightness_mode_manual); settings.system.putint(contentresolver, settings.system.screen_brightness, newbrightness); log.v(context.getstring(r.string.app_name), "batteryreceiver: setting brightness " + newbrightness + "("+ batterypercentage+ "%, " + plugged +")"); }
as might imagine, brightness setting changed correctly -if reboot device new brightness setting applied next time device reboot- it's not applied immediatly.
is there way of applying setting immediatly without having start activity? maybe api system app can use?
Comments
Post a Comment