android 8.1 disable unsupported sensor

如果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下相关的配置文件

相关推荐
FungLeo17 分钟前
Flutter/Android Release 包连不上网?AndroidManifest INTERNET 权限排查实录
android·flutter
wWYy.2 小时前
Mysql:一行数据是怎么存储的?
android·数据库·mysql
漏刻有时10 小时前
PHP GeoJSON转PNG地图渲染程序开发笔记、源码解读、问题复盘与整改方案
android·笔记·php
-SOLO-11 小时前
解决VMware 显示比例被重置的问题
android
alexhilton11 小时前
探究Android Views、Flutter和Compose如何渲染你的UI
android·kotlin·android jetpack
Lesile14 小时前
Android:Hilt框架入门 · 在ViewUI和ComposeUI下的应用
android·android jetpack
用户4238162290714 小时前
Android 16 WebView 页面顶部挖孔/通知栏不能显示UI问题排查
android
weixin_7275356215 小时前
Loop 已死,Graph 新生:AI 工作流的范式革命
android·人工智能·rxjava
严同学正在努力16 小时前
从备份到恢复:我用 30 分钟恢复了误删的核心业务表
android·java·数据库·ai
梦想三三16 小时前
LangChain Output Parser 实战:从字符串到结构化数据的完整指南
android·服务器·langchain·github·uv