如果device不支持某种sensor,可以在android/frameworks/base/core/java/android/hardware/SystemSensorManager.java里将其disabled掉。以disable proximity sensor为例。
public SystemSensorManager(Context context, Looper mainLooper) {
synchronized(sLock) {
if (!sNativeClassInited) {
sNativeClassInited = true;
nativeClassInit();
}
}
mMainLooper = mainLooper;
mTargetSdkLevel = context.getApplicationInfo().targetSdkVersion;
mContext = context;
mNativeInstance = nativeCreate(context.getOpPackageName());
// initialize the sensor list
for (int index = 0;;++index) {
Sensor sensor = new Sensor();
if (!nativeGetSensorAtIndex(mNativeInstance, sensor, index)) break;
//disable p_sensor solution: Remove proximity sensor from sensor list.
if ((sensor != null) && sensor.getType() == Sensor.TYPE_PROXIMITY){
Log.i(TAG,"skip proximity sensor");
continue;
}
mFullSensorsList.add(sensor);
mHandleToSensor.put(sensor.getHandle(), sensor);
}
}
就是将p_sensor 从mFullSensorsList中移出。这一步基本上已经work了(SensorManager#getSensorList(8)得不到p_sensor的讯息),但是如果你的code还需要通过CTS测试,那么最好在android/frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java的hasSystemFeature方法里做remove proximity sensor feature
public boolean hasSystemFeature(String name, int version) {
// allow instant applications
synchronized (mAvailableFeatures) {
final FeatureInfo feat = mAvailableFeatures.get(name);
if (feat == null) {
return false;
} else {
//remove proximity sensor feature
if(name.equals("android.hardware.sensor.proximity")){
Log.w(TAG, "skip standard proximity sensor");
return false;
}
return feat.version >= version;
}
}
}
这样一来PackageManager#hasSystemFeature(android.hardware.sensor.proximity) return false。
可用命令adb shell pm list features来检查device都support 那些功能。
或者尝试删除frameworks\native\data\etc下相关的配置文件