android 添加sensor 权限

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                 }});

4.frameworks/base/core/java/android/hardware/SensorManager.java public SensorManager(Context context,Looper mainLooper) {
        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 {

	@Override
	protected 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());
			 
		}
			         }   
	 
	

	@Override
	public 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>

android 添加sensor 权限_第1张图片


android 添加sensor 权限_第2张图片

你可能感兴趣的:(android 添加sensor 权限)