android 添加sensor 权限
16lz
2022-06-16
1.frameworks/base/core/res/AndroidManifest.xml:
<!-- Allows an application to sensor_enable xiayu --><permission android:name="android.permission.SENSOR_ENABLE"
android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
android:protectionLevel="dangerous"
android:label="@string/permlab_sensor_enable"
android:description="@string/permdesc_installLocationProvider" />
<!-- Allows an application to sensor_info xiayu -->
<permission android:name="android.permission.SENSOR_INFO"
android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
android:protectionLevel="normal"
android:label="@string/permlab_sensor_info"
android:description="@string/permdesc_installLocationProvider" />
2.frameworks/base/core/res/res/values/strings.xml
<!-- Title of an application permission, listed so the user can choose whether they want to allow the application to do this. --><string name="permlab_sensor_enable">sensor enable</string>
<!-- Title of an application permission, listed so the user can choose whether they want to allow the application to do this. -->
<string name="permlab_sensor_info">sensor info</string>
3.frameworks/base/core/java/android/app/ContextImpl.java
registerService(SENSOR_SERVICE, new ServiceFetcher() {
389 public Object createService(ContextImpl ctx) {
390 return new SensorManager(ctx,ctx.mMainThread.getHandler().getLooper());//xiayu fix
391 }});
int xiayu=0;
mMainLooper = mainLooper;
Log.e(TAG,"checking android.permission.SENSOR_ENABLE");
if(context.checkCallingOrSelfPermission("android.permission.SENSOR_ENABLE")==PackageManager.PERMISSION_GRANTED){
Log.e(TAG,"permission SENSOR_ENABLE");
xiayu=1;
}
Log.e(TAG,"checking android.permission.SENSOR_INFO");
if ( (xiayu==1) &&
(context.checkCallingOrSelfPermission("android.permission.SENSOR_INFO")
!= PackageManager.PERMISSION_GRANTED)) {
Log.e(TAG,"xiayu no android.permission.SENSOR_INFO");
throw new SecurityException(" requires SENSOR_INFO permission");
}
测试源码:
package com.example.sensor_info; import java.util.List; import com.example.sensor_info.R; import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorManager; import android.widget.TextView; import android.util.Log;import android.os.Bundle;import android.app.Activity;import android.view.Menu;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {try{super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}catch(Exception e){ Log.e("ERROR", "ERROR IN CODE: " + e.toString()); e.printStackTrace(); }final TextView tx1 = (TextView) findViewById(R.id.TextView01);//tx1.setText("xiayu xxxxxxxxxxxxxxxxxx"); try{SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE); List<Sensor> allSensors = sm.getSensorList(Sensor.TYPE_ALL);//显示有多少个传感器 tx1.setText("经检测该手机有" + allSensors.size() + "个传感器,他们分别是:\n"); for (Sensor s : allSensors) { String tempString = "\n" + " 设备名称:" + s.getName() + "\n" + " 设备版本:" + s.getVersion() + "\n" + " 供应商:" + s.getVendor() + "\n"; switch (s.getType()) { case Sensor.TYPE_ACCELEROMETER: tx1.setText(tx1.getText().toString() + s.getType() + " 加速度传感器accelerometer" + tempString); break; case Sensor.TYPE_GYROSCOPE: tx1.setText(tx1.getText().toString() + s.getType() + " 陀螺仪传感器gyroscope" + tempString); break; case Sensor.TYPE_LIGHT: tx1.setText(tx1.getText().toString() + s.getType() + " 环境光线传感器light" + tempString); break; case Sensor.TYPE_MAGNETIC_FIELD: tx1.setText(tx1.getText().toString() + s.getType() + " 电磁场传感器magnetic field" + tempString); break; case Sensor.TYPE_PRESSURE: tx1.setText(tx1.getText().toString() + s.getType() + " 压力传感器pressure" + tempString); break; case Sensor.TYPE_PROXIMITY: tx1.setText(tx1.getText().toString() + s.getType() + " 距离传感器proximity" + tempString); break; default: tx1.setText(tx1.getText().toString() + s.getType() + " 未知传感器" + tempString); break; } } }catch(Exception e){ tx1.setText("ERROR:"+e.toString()); } } @Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.activity_main, menu);return true;}}
Androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.sensor_info" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.SENSOR_INFO" /> <uses-permission android:name="android.permission.SENSOR_ENABLE" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.sensor_info.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
更多相关文章
- Ubuntu识别不到Android设备
- Android: Android(安卓)Bluetooth
- Android下USB Accessory的实现分析 (四)--- AOA上层分析
- 读取 android 设备的电池信息
- Android:制作Update.zip升级包
- Android(安卓)集成百度地图实现设备定位
- Android--学习笔记--02--AndroidStudio的设置
- android kernel和标准linux kernel的区别
- Android传感器编程入门(二)