Home  >  Q&A  >  body text

java - 安卓实现熄屏功能。

在APP中定义一个时间(假设为5分钟),记录两次操作APP之间的时间间隔,如果大于之前定义的时间,则APP端实现发出命令,手机屏幕熄灭。该功能要如何实现?

迷茫迷茫2765 days ago689

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-04-18 10:55:12

    The first point is the lock screen function. You can refer to: https://github.com/chniccs/Lo...
    This small project is the implementation of the lock screen function, with a small amount of code.

    Another point is to see how you define each operation. Here, an activity will be displayed in the foreground as an operation. You can also rewrite touch monitoring to define some touch operations as an operation.
    1. Write a BaseActivity, send a delayed message (broadcast or other handler) in onResume (this is used here to represent an operation), and record the current time (stored in a custom Application or a global singleton or persistent storage )
    2. According to the previous method of sending delayed messages, such as broadcast, after receiving the broadcast message, compare the current time value with the time value recorded in step 1. If it is less than the defined time value, take it again The difference between the two time values ​​(stored and current) is used as a delay before sending a broadcast. When the broadcast is received next time, it will be judged again. If it is greater than or equal to the time value, the lock screen will be called.

    Explanation: Why do you need to take the difference value in step 2 instead of re-sending a fixed-delay broadcast? This is because after sending the broadcast for the first time, you may perform some operations, which will be subsequent operations, that is to say When you receive a delayed broadcast, the time of the last operation is not the time recorded when the broadcast was sent, but the time of the actual last operation. Therefore, the recorded time value needs to be updated after each operation.

    Difference calculation: defined fixed maximum time of no operation - (current time - last operation time)

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 10:55:12

    Adopt Handler’s postDelayed(Runnable, long) method
    1. Define a Handler class

    Handler handler=new Handler();  
    Runnable runnable=new Runnable() {  
        @Override  
        public void run() {  
            // TODO Auto-generated method stub  
            //要做的事情  
            handler.postDelayed(this, 2000);  
        }  
    };  

    2. Start the timer

    handler.postDelayed(runnable, 2000);

    3.Stop the timer

    handler.removeCallbacks(runnable); 
    

    That’s probably the number, you decide the time

    reply
    0
  • Cancelreply