How to Learn about KeyguardManager for Android Screen Protection

Source: Internet
Author: User

A few days ago, I received a request from the customer to create an Android mobile phone and screen saver on the Panel. In fact, it is quite strange that screen saver is used for computers, but when the phone is not used, is it OK to turn off the screen? It is said that the current smart phone battery is not durable and poor, it is not very expensive to install screen saver. Originally, the customer was used to place the device in the Business Office (mobile phone-related) and use the mobile phone or tablet to project the device to an electronic screen to display the advertisement. The customer only needs to use the 24-hour continuous electricity for display, therefore, power usage is not considered. You need to upload the image to be displayed on the server. You can obtain the updated image on the PDA, as long as you keep rolling their ads. I didn't do it at first. My colleagues have already written about it. Suddenly, he was very anxious to leave other projects for me to do, by the way, let's take a look at the android screensaver knowledge and write it down for accumulation. The hacker first came into contact with KeyguardManager, which is used to block the Screen Saver Of the system. public class KeyguardManager extends Object Class that can be used to lock and unlock the keyboard. get an instance of this class by calling Context. getSystemService (java. lang. string) with argument Context. KEYGUARD_SERVICE. the actual class to control the keyboar D locking is KeyguardManager. KeyguardLock. A lock and unlock class is used to obtain the instance by calling Context. getSystemService (Context. KEYGUARD_SERVICE. In fact, KeyguardManager is used to control the screen lock. the KeyguardLock class KeyguardManager has two internal classes: (1) KeyguardManager. keyguardLock (l two functions) Remember to add the permission <uses-permission android: name = "android. permission. DISABLE_KEYGUARD "/> disableKeyguard () function to unlock the reenableKeyguard () function. reenableKeyguard () unlocks the screen. If the screen is locked before the disableKeyguard () function is called, the screen is locked. Otherwise, no operation is performed. Of course, if the disableKeyguard () function is not called before, no operation is performed. (2) KeyguardManager. onKeyguardExitResult (boolean success, it is used for judgment and debugging. It is estimated that> after the crash, the screen wake-up content is enabled and disabled. This part of PowerManager and WakeLock has been used before and is not described in detail, this time, this part is written as a tool class to simplify the Code [java] package com. eyu. screen. util; import android. content. context; import android. OS. powerManage R; import android. OS. powerManager. wakeLock; public class PowerManagerWakeLock {private static WakeLock wakeLock;/** enable to keep the screen awake */public static void acquire (Context context) {PowerManager pm = (PowerManager) context. getSystemService (Context. POWER_SERVICE); wakeLock = pm. newWakeLock (PowerManager. SCREEN_BRIGHT_WAKE_LOCK | PowerManager. ON_AFTER_RELEASE, "PowerManagerWakeLock"); wakeLock. acquire ();}/* * Disable keep screen wake-up */public static void release () {if (wakeLock! = Null) {wakeLock. release (); wakeLock = null ;}} finally ------------ in fact, the most important thing is the above content. The next step is to write the Service and send broadcast operations, and implement communication with the server. After the installation, the server end and the updated content are left aside and the updated content is downloaded. The basic idea and operation are the same as that of the company project, but the images must be manually added, after use, the system constantly monitors the status of the screen. Once the screen is destroyed, it will immediately wake up. The viewpager and timer are used to control the automatic page turning of the image. If you want to completely stop the screen, you need to disable the background service, the following is a small program. After the program is started, it will first make a judgment. If the directory specified by the SD card does not contain images, the resource file will be loaded, and images will be loaded into the SD card.

 

Service Code: [java] package com. eyu. screen. UI; import com. eyu. screen. util. powerManagerWakeLock; import android. app. keyguardManager; import android. app. service; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. content. intentFilter; import android. OS. IBinder; public class ScreenSaverS extends Service {@ Override public IBinder onBin D (Intent intent) {// TODO Auto-generated method stub return null;} public void onCreate () {// shield the system's screensaver KeyguardManager manager = (KeyguardManager) getSystemService (Context. KEYGUARD_SERVICE); KeyguardManager. keyguardLock lock = manager. newKeyguardLock ("KeyguardLock"); lock. disableKeyguard (); // registers a listener screen to enable and disable the broadcast IntentFilter filter = new IntentFilter (); filter. addAction (Intent. ACTION_SCREEN_ON ); Filter. addAction (Intent. ACTION_SCREEN_OFF); registerReceiver (screenReceiver, filter);} BroadcastReceiver screenReceiver = new BroadcastReceiver () {@ Override public void onReceive (Context context, Intent intent) {// TODO Auto-generated method stub String action = intent. getAction (); if (action. equals (Intent. ACTION_SCREEN_ON) {} else if (action. equals (Intent. ACTION_SCREEN_OFF) {// If the screen is closed If (! ScreenSaverShowA. isShow) {// enable screen wake-up, always on PowerManagerWakeLock. acquire (ScreenSaverS. this);} PowerManagerWakeLock. acquire (ScreenSaverS. this); Intent intent2 = new Intent (ScreenSaverS. this, ScreenSaverShowA. class); intent2.setFlags (Intent. FLAG_ACTIVITY_NEW_TASK); startActivity (intent2); PowerManagerWakeLock. release () ;}}; public void onDestroy () {PowerManagerWakeLock. release (); unregisterReceiver (s CreenReceiver) ;}}the Screen Saver display Page code: [java] <span style = "font-size: 12px;"> package com. eyu. screen. UI; import java. io. file; import java. util. timer; import java. util. timerTask; import com. eyu. screen. r; import com. eyu. screen. adapter. screenSaverShowAdapter; import com. eyu. screen. util. folderUtil; import android. annotation. suppressLint; import android. app. activity; import android. graphics. bitmap; import android. Graphics. bitmapFactory; import android. graphics. drawable. drawable; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. support. v4.view. viewPager; import android. util. log; import android. view. view; import android. view. view. onClickListener; @ SuppressLint ("HandlerLeak") public class ScreenSaverShowA extends Activity {public static boolean isShow = false; p Rivate ViewPager viewPager; private Bitmap [] bmp s = null; private Drawable [] drawables = new Drawable [5]; @ Override protected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stub super. onCreate (savedInstanceState); setContentView (R. layout. screenshow); isShow = true; initControl (); autoSwitch () ;}@ Override protected void onDestroy () {// TODO Auto-generated method stub isS How = false; // recycle bitmap to avoid OOM if (BMP s! = Null) {for (int I = 0; I <BMP S. length; I ++) {BMP s [I]. recycle () ;}} else {return;} super. onDestroy ();} private void initControl () {findViewById (R. id. btn_jiesuo ). setOnClickListener (new OnClick (); viewPager = (ViewPager) findViewById (R. id. viewpager); File f = new File (FolderUtil. getSaveFolder (); File [] files = f. listFiles (); // obtain all the subdirectories BMP = new Bitmap [files. length]; // If the folder is empty, the image I is loaded from the resource file F (files. length = 0) {drawables [0] = getResources (). getDrawable (R. drawable. bg_01); drawables [1] = getResources (). getDrawable (R. drawable. bg_02); drawables [2] = getResources (). getDrawable (R. drawable. bg_03); drawables [3] = getResources (). getDrawable (R. drawable. bg_04); drawables [4] = getResources (). getDrawable (R. drawable. bg_05); ScreenSaverShowAdapter adapter = new ScreenSaverShowAdapter (ScreenSave RShowA. this, drawables); viewPager. setAdapter (adapter);} else {// If the folder is not empty, the image for (int I = 0; I <files. length; I ++) {String path = files [I]. getAbsolutePath (); BMP s [I] = BitmapFactory. decodeFile (path); Log. d ("PDA", "=== H ===" + path);} ScreenSaverShowAdapter adapter = new ScreenSaverShowAdapter (ScreenSaverShowA. this, bmp s); viewPager. setAdapter (adapter) ;}/ ** automatic switching of images at regular intervals */private Void autoSwitch () {int interval = 3000; Timer timer = new Timer (); TimerTask task = new TimerTask () {@ Override public void run () {// TODO Auto-generated method stub Message message = new Message (); handler. sendMessage (message) ;}}; timer. schedule (task, interval, interval);} Handler handler = new Handler () {@ Override public void handleMessage (Message msg) {int currentPage = viewPager. getCur RentItem (); int tempNum = bmp s. length = 0? Drawables. length: bmp s. length; int nextPage = (currentPage + 1) % tempNum; viewPager. setCurrentItem (nextPage); super. handleMessage (msg) ;}}; class OnClick implements OnClickListener {@ Override public void onClick (View v) {// TODO Auto-generated method stub ScreenSaverShowA. this. conclusion and Note: 1. system screen saver shielding methods and screen wake-up knowledge can be referred to the API, a relatively simple new knowledge 2. if many images are to be loaded, it is likely that the most troublesome OOM occurs. Screen Saver requires full screen display, so the image is scaled. The method is not suitable. I did not expect a good solution. However, in order to avoid OOM during repeated unlocking, the image is recycle in the onDestroy () method. 3. no problem is found when running on the simulator, but problems may occur on different models. For example, some mobile phones have changed the android source code, and even do not allow screen blocking. It's still the best for the original android system.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.