Android--distance sensor control screen white screen

Source: Internet
Author: User

Permissions

<android:name= "Android.permission.DEVICE_POWER"/><  android:name= "Android.permission.WAKE_LOCK"/>

The first permission to add to the XML file will be error, but I will be very neat to clean off, the implementation of steps: Find the Eclipse menu options Project>clean, select the current project is OK.

Code

 Public classMainactivityextendsActivityImplementsSensoreventlistener { Public Static FinalString TAG = "Sensortest"; //call distance sensor, control screen    PrivateSensormanager Mmanager;//Sensor Management Objects//Screen Switch    PrivatePowerManager Localpowermanager =NULL;//Power Management Objects    PrivatePowermanager.wakelock Localwakelock =NULL;//Power Lock//TextView    PrivateTextView TV;//It's basically useless .@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main); TV=(TextView) Findviewbyid (r.id.tv); Mmanager=(Sensormanager) Getsystemservice (Context.sensor_service); //Get system service Power_service, return a PowerManager objectLocalpowermanager =(PowerManager) Getsystemservice (Context.power_service); //gets the Powermanager.wakelock object, followed by the argument | To pass in two values at the same time, and finally the tag used in Logcat .Localwakelock = This. Localpowermanager.newwakelock (+, "mypower");//The first parameter is the power lock level, the second is the log tag    }     Public voidOnresume () {Super. Onresume (); Mmanager.registerlistener ( This, Mmanager.getdefaultsensor (sensor.type_proximity),//Distance sensorSensormanager.sensor_delay_normal);//register the sensor, the first parameter is the distance listener, the second is the sensor type, the third is the delay type    }     Public voidOnStop () {Super. OnStop (); LOG.D (TAG,"On Stop"); }     Public voidOnDestroy () {Super. OnDestroy (); LOG.D (TAG,"On Destroy"); if(Mmanager! =NULL) {localwakelock.release ();//release the Power lock, if not release finish this acitivity will still have the effect of automatic lock screen, do not believe you can tryMmanager.unregisterlistener ( This);//Logoff sensor monitoring}} @Override Public voidonsensorchanged (Sensorevent event) {float[] its =event.values; //log.d (TAG, "its array:" +its+ "Sensor type:" +event.sensor.gettype () + "proximity type:" +sensor.type_proximity);        if(Its! =NULL&& Event.sensor.getType () = =sensor.type_proximity) {System.out.println ("Its[0]:" + its[0]); Tv.settext (its[0]+ ""); //after testing, when the hand is close to the distance sensor Its[0] return value is 0.0, when the hand is left to return 1.0            if(Its[0] = = 0.0) {//Close to the phoneSystem.out.println ("Hands Up"); LOG.D (TAG,"Hands up in calling activity"); if(Localwakelock.isheld ()) {return; } Else{localwakelock.acquire ();//Request Device Power lock                }            } Else{//away from the phoneSystem.out.println ("Hands moved"); LOG.D (TAG,"Hands moved in calling activity"); if(Localwakelock.isheld ()) {return; } Else{localwakelock.setreferencecounted (false); Localwakelock.release (); //release the device power lock} }} @Override Public voidOnaccuracychanged (sensor sensor,intaccuracy) {        //TODO Auto-generated method stubs    }}

It is important to note that in the OnDestroy () function, you need to release the object Lock (Localwakelock.release ()), otherwise this function will persist until you know that you have applied the data or deleted the application, I have been troubled by this problem for a long time. For this reason I deliberately made a jump page in the source code example, adding a log.

Here we instantiate two management objects, one is the Sensor management object: Sensormanager; the other is the power management object: Localpowermanager.

The sensor management object collects and collects the distance data between objects, and the power management object handles the screen and white screen by judging the data collected by the sensor.

I'm the dividing line of the king of the Land Tiger.

Source code: HTTP://PAN.BAIDU.COM/S/1DD1QX01

Distance sensor. zip

Reference: http://blog.csdn.net/luozhi3527/article/details/9999901

Related Article

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.