As the app realizes more and more functions, watching the novel video surfing the Internet and so on, many people now spend more and more time on mobile devices such as mobile phone tablets. But the phone and tablet screen is not like the Kindle that type of paper book Ink screen so engaging, because of the self-luminous screen characteristics, we stare at the screen for a long time easy to see pain tired, so a variety of protection mode, night mode in mobile app is widely used, which is indeed a thoughtful small function. So this time we explore the next several ways of realization, together learn to summarize under:
1, using the screen brightness
It is also the simplest and most effective way to reduce the brightness of the screen by using a terminal such as a mobile phone at night to reduce the intensity of light.
Please add the appropriate permissions first:
<uses-permission android:name= "Android.permission.WRITE_SETTINGS"/>
There are two ways to achieve this by setting the screen brightness:
1) only set the brightness within the application
Typically, each activity in Android corresponds to a visual interface that sets the brightness for each activity, as follows:
public static void setbrightness (activity activity, float Brightnessvalue) { Windowmanager.layoutparams LP = Activity.getwindow (). GetAttributes (); if (Brightnessvalue > 1.0f) { lp.screenbrightness = 1.0f; } else if (brightnessvalue <= 0.0f) { lp.screenbrightness = 0.0f; } else { lp.screenbrightness = brightnessvalue; } Activity.getwindow (). SetAttributes (LP); }
So, a program contains all the activity, we have to set its brightness alone, although it can be encapsulated as a tool to use, but the premise is there a better way? See the second type:
2) Set the brightness of the phone system (global brightness)
Here, we are directly in the program of an activity, such as the entry activity to set the brightness of the entire phone. Since the global brightness of the phone has been set, then regardless of jump to which interface, or even quit the program, the brightness of the phone is still set brightness. This method is "once and for all" in relation to the first. But here we have to sort out the idea and consider several points:
After you open the app, get the original brightness value of your phone and save it (you can return to normal brightness after exiting the app)---> Turn off auto-adjust if your phone turns on auto-brightness, then set the appropriate lower brightness---> apply the Set brightness value to your phone---> Finally, exit the app Restore the original brightness with the saved original brightness value and turn the auto brightness adjustment back on.
Next, paste the key code:
The first is to get the screen brightness value of the phone:
/** * Get current system brightness * <br> get failed return-1, get successful return normal non-negative number <br> * @param context * @return * * public static int Getsystembrightness (context context) { int brightnessvalue =-1; Try { brightnessvalue = Settings.system. GetInt (Context.getcontentresolver (), Settings.System.SCREEN_BRIGHTNESS); } catch (Exception e) { e.printstacktrace (); } return brightnessvalue; }
Save, put directly in the sharepreference inside is good, the related code will not write.
Then detect if the phone turns on the brightness auto-adjust switch:
/** * Whether to turn on auto adjust brightness * @param contentresolver * @return * * /Public static Boolean isautobrightness ( Contentresolver contentresolver) { Boolean autobrightness = false; Try { autobrightness = Settings.System.getInt (contentresolver, Settings.System.SCREEN_ Brightness_mode) = = Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC; } catch (Exception e) { e.printstacktrace (); } return autobrightness; }
If you open it, close it:
/** * Stop auto-adjust brightness * @param activity * /public static void closeautobrightness (activity activity) { Settings.System.putInt (Activity.getcontentresolver (), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); }
Finally, you'll use the turn-on brightness adjustment:
public static void openautobrightness (activity activity) { Settings.System.putInt ( Activity.getcontentresolver (), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_ brightness_mode_automatic); }
When you turn off brightness adjustment, set the brightness to use the relevant code in the first method directly. But because you want to apply this brightness setting to the global, you need to save that brightness value to your phone:
/** * Save Global Brightness value setting * @param contentresolver * @param brightnessvalue Brightness value */public static void Savebrightness (contentresolver contentresolver, int brightnessvalue) { uri uri = android.provider. Settings.System.getUriFor ("screen_brightness"); Android.provider.Settings.System.putInt (Contentresolver, "screen_brightness", brightnessvalue); Contentresolver.notifychange (URI, NULL); }
In this way, the final effect is OK, even if exiting the current application, the phone is still set to the lower brightness. Of course, you should restore normal brightness and settings before exiting the application, then use the saved brightness value to reset, and then save the new brightness value again to the phone, do not forget that the phone is turned on automatically adjust the brightness, but also to turn on automatic adjustment.
2, custom theme. (the most common method)
Custom view, I believe a lot of people are very familiar with. The custom theme, like this one, is the most common way to implement night mode because it not only implements night mode, but also implements common theme-changing functions. Here is not to elaborate, only to talk about ideas. Suppose our application interface is white background, black text, night mode is black background, gray-white text. This night mode is different from the first adjustment brightness, because the background and content text can be arbitrarily set color and transparency, this night mode looks more intuitive, can also be more comfortable.
The custom theme is used to define the background and text color attributes to be used in XML, such as:
<declare-styleable name= "Mythemeattrs" > <attr name= "activity_background" format= "Color"/> <attr name= "Text_color" format= "color"/> </declare-styleable>
Then create your own two themes (Theme) in Style.xml, such as the default theme and night theme, set the Activity_background property to white in the default theme, the Text_color property to Black, and the night theme to black and gray, respectively. In the view layout file, give the background view used, such as the BackgroundColor property of a relativelayout set to "? Activity_background", TextView TextColor set to "? Text_color "can. Of course, since this is theme, the app will take effect when the activity starts initializing the view. It is therefore best to encapsulate a theme tool class yourself by calling the Settheme () method before the activity's Setcontentview () method to set the theme.
3, WindowManager implement mask mode
Here, we should understand a concept when it is not necessary to delve into window. Android's design concept, to give almost every display component is set to include in a window. The activity also has its own window. By adding a layer of gray to the window with a certain degree of transparency of the view, so that it appears to be dimmed, of course, the brightness of the phone is not changed, this implementation, can be called "mask", similar to the camera when photographed in the lens set a layer of film or lens up, so that the effect of rendering different. But there is a downside to this approach, which is similar to what is said above to set its brightness individually in each activity. Here every entry into an interface needs to re-"set a layer of view" up, relative to the "once and for all" approach, it appears that there is no advantage. Then directly on the code:
WindowManager manager = (WindowManager) getsystemservice (context.window_service); Windowmanager.layoutparams params = new Windowmanager.layoutparams ( WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_APPLICATION, windowmanager.layoutparams.flag_not_touchable| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, pixelformat.translucent); params.gravity = Gravity.top; Params.y = 10;//distance from the bottom is 10 pixels if top is the distance top is 10 pixels TextView TV = new TextView (this); Tv.setbackgroundcolor (0x55000000); Manager.addview (Tv,params);
In the code, the touch and focus of the added matte layer is forbidden by the Windowmanager.layoutparams parameter setting. This allows you to add a layer of view without affecting the normal use of the components in the activity view.
In fact, the above three methods, placed in the present, are not new technology, and online I have seen a lot of relevant code, here is just put together to do a comparison and summary. The above according to three methods of implementation of the respective characteristics, in general, the first, two methods are preferable, and the choice is the first or the second method, should look at the specific needs, if your application is simply a request to reduce brightness, do not want to modify too much code, then the first will be more appropriate; If you want to have a good experience, want to look cool, and even want to add other topics, such as blue, green themes and so on, then undoubtedly the second is the best choice. The wide application of custom attributes provides the convenience for us to achieve more personalized visuals, such as custom components, custom themes, and more.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Implements Night Mode summary