Android distance sensor controls screen off white screen

Source: Internet
Author: User

Recently, I am working on a network call APK, which features the same SIM card phone number as the mobile phone number. Although only Java is used, many problems are also encountered. One of them is to simulate the status of the call and control the screen lock. I know it is achieved through the distance sensor on the mobile phone, but after a long time, I finally got the result and shared it with me. : Http://download.csdn.net/detail/luozhi3527/5959023

When I posted a post on EOE to ask this question, Daniel told me that the screen can be lit, but the screen removal requires system permissions and should not be able to do so. He is right. here we do need to apply for permissions:

<Uses-Permission Android: Name = "android. Permission. device_power"/>
<Uses-Permission Android: Name = "android. Permission. wake_lock"/>

When the first permission is added to the XML file, an error will be reported, but I will easily clean it. implementation step: Find the menu option project> clean in eclipse and select the current project.

Next, let's look at the code of the next class:

Package COM. xluo. sensortest; import android. app. activity; import android. content. context; import android. hardware. sensor; import android. hardware. sensorevent; import android. hardware. sensoreventlistener; import android. hardware. sensormanager; import android. OS. bundle; import android. OS. powermanager; import android. util. log; import android. view. menu; import android. view. view; public class sensortest extends Act Ivity implements sensoreventlistener {public static final string tag = "sensortest"; // call the distance sensor to control the screen's private sensormanager mmanager; // sensor management object // screen switch private powermanager localpowermanager = NULL; // power management object private powermanager. wakelock localwakelock = NULL; // power lock @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. sensor); mmanager = (sensorm Anager) getsystemservice (context. sensor_service); // obtain the power_service of the System Service. A powermanager object localpowermanager = (powermanager) getsystemservice (context. power_service); // obtain powermanager. wakelock object, followed by the parameter | indicates that two values are passed in at the same time, and finally the tag localwakelock = This used in logcat. localpowermanager. newwakelock (32, "mypower"); // The first parameter is the power lock level, and the second parameter is the log tag} public void onresume () {super. onresume (); mmanager. registerlistener (this, mmanager. getde Faultsensor (sensor. type_proximity), // distance sensor sensormanager. sensor_delay_normal); // register the sensor. The first parameter is the distance listener, the second parameter is the sensor type, and the third parameter is the delay type.} public void onstop () {super. onstop (); log. D (TAG, "On stop");} public void ondestroy () {super. ondestroy (); log. D (TAG, "On Destroy"); If (mmanager! = NULL) {localwakelock. release (); // release the power lock. If you do not release the acitivity, the lock will still be automatically locked. If you do not believe it, try mmanager. unregisterlistener (this); // deregister the sensor listener} @ overridepublic void onaccuracychanged (sensor, int accuracy) {// todo auto-generated method stub} @ overridepublic void onsensorchanged (sensorevent event) {// todo auto-generated method stubfloat [] its = event. values; // log. D (TAG, "its array:" + its + "sensor type:" + event. s Ensor. GetType () + "proximity type:" + sensor. type_proximity); If (its! = NULL & event. sensor. getType () = sensor. type_proximity) {system. out. println ("its [0]:" + its [0]); // after testing, the value of its [0] returned when the close sensor is attached to the hand is 0.0, returns 1.0if (its [0] = 0.0) when the hand leaves {// close to the mobile phone system. out. println ("Hands up"); log. D (TAG, "Hands up in calling activity"); If (localwakelock. isheld () {return;} else {localwakelock. acquire (); // apply for a device power lock} else {// stay away from the mobile phone system. out. println ("hands moved"); log. D (TAG, "hands moved in calling activity"); If (localwakelock. isheld () {return;} else {localwakelock. setreferencecounted (false); localwakelock. release (); // release the power lock of the device }}}}}

Of course, the example is just to implement the screen lock without the call function. But the principle is probably like this. When a call or call is made, the phone will switch to a call interface, which should have the function described above.

Note that the ondestroy () function needs to release the object lock (localwakelock. release (), otherwise this feature will always be known that you are clear about the application data or delete the application, and I am suffering from this problem for a long time. For this reason, I specifically made a jump page in the source code example and added log.

Here we instantiate two management objects, one is sensor management object: sensormanager; the other is power management object: localpowermanager.

Sensor Management objects are responsible for collecting distance data between objects, and power management objects process screens and white screens by judging the data collected by sensors.

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.