Android-Remove screen white screen from sensor control
Permission <uses-permission android: name = "android. permission. DEVICE_POWER "/> <uses-permission android: name =" android. permission. WAKE_LOCK "/> An error will be reported when the first permission is added to the xml file, but I will easily clean it. Implementation steps: Find the menu option project> clean in eclipse, select the current project. Code replication code public class MainActivity extends Activity implements SensorEventListener {public static final String TAG = "SensorTest"; // call the distance sensor to control the screen private SensorManager mManager; // sensor management object // screen switch private PowerManager localPowerManager = null; // power management object private PowerManager. wakeLock localWakeLock = null; // power lock // TextView private TextView TV; // basically no @ Override protected void onCreate (Bundle savedInstanceS Tate) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); TV = (TextView) findViewById (R. id. TV); mManager = (SensorManager) 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. localPower Manager. 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. getdefasensensor (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} @ Override public void onSensorChanged (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 close sensor is attached to the hand, its [0] returns 0.0, 1.0 if (its [0] = 0.0) is returned 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 }}}@ Override public void onAccuracyChanged (Sensor sensor, int accuracy) {// method stub automatically generated by TODO} note that the object lock must be released (localWakeLock) in the onDestroy () function. 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.