Android realizes the function of keeping the screen always on

The app made by the author's company is stock. When viewing the stock quotation page, users often need to turn on the target mode. At this time, the screen cannot be black. The black screen will cause users to not see the rise and fall of some key quotations. If they miss the best time to buy and sell, they will cause losses to users, which is not allowed by stock software, Therefore, general stock apps will have the function of constant on screen.

When we did this function, we found some tutorials on the Internet and found that some did not achieve the effect. Then we found a perfect implementation scheme without compatibility. Let's share it with you.

There is a solution on the Internet to use PowerManager to realize that the screen is not locked:

However, in the actual test process, this method does not achieve the effect that the screen is always on, and the permission needs to be declared, otherwise it will crash, so this method is passed:

Later it was discovered that the function of constant brightness is very simple. It only needs to get the Window object in the current Activity and then call its addFlags method plus a WindowManager.LayoutParams.FLAG_. KEEP_ SCREEN_ Identification of on.

Then, when the constant light function is turned off, you only need window to clear the windowmanager.layoutparams.flag_ KEEP_ SCREEN_ On mark is enough

So we simply encapsulate it. As long as the page activity wants the screen to be always on, it can call the following method:

In the onCreate () method of Activity that wants to keep the screen constantly lit, the following method can be called:

Because the screen always on function can be set in the settings, and the user needs to save the previous settings next time he enters the app, we saved whether to turn on the always on function in SharedPreferences.

What should I do if the screen of the whole app should be always on? Does oncreate() in all activities write and call this method? The answer is obviously not. It's too inefficient.

In general, our project will have parent classes such as baseactivity and basefragment to extract general methods and style specifications, so we can judge whether the screen constant light function is required in the baseactivity inherited by all activities. In this way, its subclasses have this function, such as:

In addition, not all activities in our app inherit baseactivity. For example, we need to use H5 and native interaction for some pages. In order to make interaction more convenient and safe, Cordova is generally selected, and the activity responsible for opening H5 pages needs to inherit Cordova activity in order to realize interaction. Cordovaactivity is a third-party activity. Obviously, it has nothing to do with our baseactivity.

At the same time, if we integrate some third-party SDKs and open the pages in their SDKs, what should we do if the screen is always on? Because we can't modify their code, we can't add the function of always on screen in their activity.

In fact, there is a very black technology function at this time, which you may not have seen before. That is, there is a method in the application, registeractivitylifecyclecallbacks, which can pass in a callback interface, which contains the life cycle method callbacks of all activities in the current app, and can obtain all activity instances, In this way, we can realize that all app pages can keep the screen on:

We can use ActivityLifecycleCallbacks's onActivityResumed method to invoke the method that the screen is always bright. The registerActivityLifecycleCallbacks method in Application should be more commonly used in the hotfix framework. It is very practical.

Finally, the example project address with the screen always on is attached. Friends in need can go and have a look. https://github.com/finddreams/ScreenLongLight

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>