Android Development: Changing Screen Brightness in Service

我们两清 提交于 2019-12-19 09:04:05

问题


Now i do try again.

I want to change the screen brightness.

I've Tried:

WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = 0.5F; // set 50% brightness
getWindow().setAttributes(layoutParams);

and seems to work in activity but when I am in service i get that getWindow() compile error.


回答1:


A service cannot change the screen brightness that way. A service does not have a user interface, so it does not have Window.

You can try to change the brightness system-wide via the SCREEN_BRIGHTNESS system setting. I have no idea if this works, as I have not tried it.

Otherwise, modify your activities to change their brightness.




回答2:


You would need to start an activity from your service, all you need to do is this:

Intent myIntent = new Intent(getBaseContext(), MyActivity.class);
getApplication().startActivity(myIntent);

where MyActivity is the activity which you want to start. Inside that activity you can put code to change the window brightness, it will work.

I've included the complete code here: https://stackoverflow.com/a/9848456/1204377

Let me know if it's still unclear.



来源:https://stackoverflow.com/questions/6565054/android-development-changing-screen-brightness-in-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!