Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement a screensaver in Android

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, the editor will share with you the relevant knowledge points about how to achieve a screensaver program in Android. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's learn about it.

1. Register a BroadcastReceiver to receive ACTION_SCREEN_OFF events. When this event is received, open the custom screensaver screen. The following code is placed in MainActivity.java for testing convenience, or you can put the code in service as needed.

RegisterReceiver (mMasterResetReciever,newIntentFilter (Intent.ACTION_SCREEN_OFF)); BroadcastReceiver mMasterResetReciever=newBroadcastReceiver () {publicvoidonReceive (Context context, Intent intent) {try {Intent I = newIntent (); i.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK); i.setClass (context, ScreenSaverActivity.class); context.startActivity (I) } catch (Exception e) {Log.i ("Output:", e.toString ());}

2. When this step is completed, although the screensaver screen is displayed when the screen is closed, the screen has darkened because the ACTION_SCREEN_OFF event is received, so the screen needs to be lit forcefully at this time.

First, add permission permissions to AndroidManifest.xml:

The following code is in ScreenSaverActivity.onCreate

PowerManager pm = (PowerManager) getSystemService (POWER_SERVICE); mWakeLock = pm.newWakeLock (PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "SimpleTimer")

Then, when the screensaver screen is displayed, light up the screen. Add to ScreenSaverActivity.onResume

MWakeLock.acquire ()

In order to be able to turn off the screen when exiting the screensaver, we must release WakeLock. So add it to ScreenSaverActivity.onPause

MWakeLock.acquire ()

Note that it must be placed in the onResume, such as somewhere else. It will cause the program to fail to enter the screensaver again for unknown reasons. Since there is only one mobile phone on hand, extensive testing cannot be carried out.

3. After completing the second step, it is usually done. Press the button to return to the program. But sometimes you need to click on the screen to return to the system. At this time, you will find that no matter how much you click on the screen, the system will not respond because the screen is locked. Therefore, to achieve this function, you need the locking function of the key screen when you turn on the screensaver.

First, add permission permissions to AndroidManifest.xml:

Again, add the following code to the appropriate place. As long as it can be called when the screensaver is started

KeyguardManager mKeyguardManager= (KeyguardManager) getSystemService (Context.KEYGUARD_SERVICE); KeyguardLock mKeyguardLock= mKeyguardManager.newKeyguardLock (TAG); mKeyguardLock.disableKeyguard ()

Of course, you can also turn on the screen locking function by using the following code.

MKeyguardLock.reenableKeyguard (); these are all the contents of the article "how to implement a screensaver in Android". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report