一、底层的驱动在这里不提,只关心上层。
二、实现AndroidR 获取接近传感器Proximity Sensor的信号,休眠和唤醒的方法:获取SENSOR_SERVICE服务,侦听Sensor事件。
三、修改的代码,在frameworks\base\services\core\java\com\android\server\policy\PhoneWindowManager.java 文件里面添加下面的代码。
diff --git a/services/core/java/com/android/server/policy/PhoneWindowManager.java b/services/core/java/com/android/server/policy/PhoneWindowManager.java
index d966f36..97b2ef8 100755
--- a/services/core/java/com/android/server/policy/PhoneWindowManager.java
+++ b/services/core/java/com/android/server/policy/PhoneWindowManager.java
@@ -123,6 +123,11 @@ import android.hardware.hdmi.HdmiControlManager;
import android.hardware.hdmi.HdmiPlaybackClient;
import android.hardware.hdmi.HdmiPlaybackClient.OneTouchPlayCallback;
import android.hardware.input.InputManagerInternal;
+import android.hardware.Sensor;
+import android.hardware.SensorEvent;
+import android.hardware.SensorEventListener;
+import android.hardware.SensorManager;
+
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.AudioManagerInternal;
@@ -229,6 +234,7 @@ import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.List;
+import android.util.Log;
/**
* WindowManagerPolicy implementation for the Android phone UI. This
@@ -378,6 +384,9 @@ public class PhoneWindowManager implements WindowManagerPolicy {
BurnInProtectionHelper mBurnInProtectionHelper;
private DisplayFoldController mDisplayFoldController;
AppOpsManager mAppOpsManager;
+ private Sensor sensor;
+ private SensorManager sm;
+ private SensorEventListener listener;
PackageManager mPackageManager;
private boolean mHasFeatureAuto;
private boolean mHasFeatureWatch;
@@ -1924,6 +1933,37 @@ public class PhoneWindowManager implements WindowManagerPolicy {
"PhoneWindowManager.mBroadcastWakeLock");
mPowerKeyWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"PhoneWindowManager.mPowerKeyWakeLock");
+
+ sm=(SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
+ sensor=sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
+
+ listener=new SensorEventListener() {
+
+ @Override
+ public void onSensorChanged(SensorEvent event) {
+ // TODO Auto-generated method stub
+ //获得距离传感器中的数值,这里只有一个距离
+ float[] values = event.values;
+ Log.i(TAG,"onSensorChanged values[0]="+values[0]+" maxrange="+sensor.getMaximumRange());
+
+ if (values[0] == 0.0) {// 贴近手机
+ mPowerManager.goToSleep(SystemClock.uptimeMillis());
+ Log.d(TAG, "The object is near to sensor ! go to sleep now");
+ } else {// 远离手机
+ wakeUpFromPowerKey(SystemClock.uptimeMillis());
+ Log.d(TAG, "The object is far to sensor !");
+
+ }
+ }
+
+ @Override
+ public void onAccuracyChanged(Sensor sensor, int accuracy) {
+ // TODO Auto-generated method stub
+ Log.i(TAG,"onAccuracyChanged");
+ }
+ };
+
+ sm.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_NORMAL);
mEnableShiftMenuBugReports = "1".equals(SystemProperties.get("ro.debuggable"));
mLidKeyboardAccessibility = mContext.getResources().getInteger(
com.android.internal.R.integer.config_lidKeyboardAccessibility);
四、用手指接近传感器的时候会灭屏休眠,手指离开的时候亮屏唤醒,查看log如下: