简体   繁体   English

Android开发:在服务中改变屏幕亮度

[英]Android Development: Changing Screen Brightness in Service

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.并且似乎在活动中工作,但是当我在服务中时,我得到了getWindow()编译错误。

A service cannot change the screen brightness that way.服务无法以这种方式更改屏幕亮度。 A service does not have a user interface, so it does not have Window .服务没有用户界面,因此它没有Window

You can try to change the brightness system-wide via the SCREEN_BRIGHTNESS system setting.您可以尝试通过SCREEN_BRIGHTNESS系统设置更改整个系统的亮度。 I have no idea if this works, as I have not tried it.我不知道这是否有效,因为我没有尝试过。

Otherwise, modify your activities to change their brightness.否则,请修改您的活动以更改其亮度。

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.其中 MyActivity 是您要开始的活动。 Inside that activity you can put code to change the window brightness, it will work.在该活动中,您可以输入代码来更改 window 亮度,它将起作用。

I've included the complete code here: https://stackoverflow.com/a/9848456/1204377我在这里包含了完整的代码: https://stackoverflow.com/a/9848456/1204377

Let me know if it's still unclear.如果还不清楚,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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