android中自带传感器的接口与函数,主要有加速度、方向、压力、光、温度传感器,不同android机可能有的传感器没有,可以使用PC机和虚拟机互联的方式来模拟,此方法主要是调用模拟传感器的接口与函数,两者无大区别。本例程建立在真机调试的情况。

    1.activity代码如下:

package com.example.sensortest;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.widget.EditText;

public class SensorTest extends Activity implements SensorEventListener
{
	private SensorManager  mSensorManager;
	EditText etAccelerometer;
	EditText etMagnetic;
	//EditText etTemperature;
	EditText etLight;
	EditText etPressure;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sensor_test);
		//获取id
		etAccelerometer =(EditText) findViewById(R.id.etAccelerometer);
		etMagnetic =(EditText) findViewById(R.id.etMagnetic);
		//etTemperature =(EditText) findViewById(R.id.etTemerature);
		
		etLight =(EditText) findViewById(R.id.etLight);
		etPressure =(EditText) findViewById(R.id.etPressure);
		//获取真机的sensor服务
		 
		mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
	 }

	//@SuppressWarnings("deprecation")
	@Override
	protected void onResume()
	{ 
		super.onResume();
		mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_GAME);
		mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_GAME);
		//	mSensorManager.registerListener(this, mSensorManager.get(Sensor.TYPE_TEMPERATURE), SensorManager.SENSOR_DELAY_GAME);
		mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT), SensorManager.SENSOR_DELAY_GAME);
		mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE), SensorManager.SENSOR_DELAY_GAME);
		
	}
	@Override
	protected void onStop()
	{
		mSensorManager.unregisterListener(this);
	    super.onStop();
	}
	
	/*
	 @Override
	protected void onPause()
	{
		mSensorManager.unregisterListener(this);
		super.onPause();
	}
	*/
	
	@Override
	public void onAccuracyChanged(Sensor sensor,int accuracy)
	{
		
	}
	
	public void onSensorChanged(SensorEvent event)
	{
		float[] values =event.values;
		int sensorType = event.sensor.getType();
		StringBuilder sb = null;
		switch(sensorType)
		{
		case Sensor.TYPE_ACCELEROMETER:
			sb = new StringBuilder();
			sb.append("绕Z转过的角度");
			sb.append(values[0]);
			sb.append("\n绕X转过的角度");
			sb.append(values[1]);
			sb.append("\n绕Y转过的角度");
			sb.append(values[2]);
			etAccelerometer.setText(sb.toString());
			break;
		case Sensor.TYPE_MAGNETIC_FIELD:
			sb = new StringBuilder();
			sb.append("X方向的角度");
			sb.append(values[0]);
			sb.append("\nY方向的角度");
			sb.append(values[1]);
			sb.append("\nZ方向的角度");
			sb.append(values[2]);
			etMagnetic.setText(sb.toString());
			break;
	/*	case Sensor.TYPE_TEMPERATURE:
			sb = new StringBuilder();
			sb.append("当前温度为:");
			sb.append(values[0]);
			etTemperature.setText(sb.toString());
			break;*/
		case Sensor.TYPE_LIGHT:
			sb = new StringBuilder();
			sb.append("当前光强:");
			sb.append(values[0]);
			etLight.setText(sb.toString());
			break;
		case Sensor.TYPE_PRESSURE:
			sb = new StringBuilder();
			sb.append("当前压力为:");
			sb.append(values[0]);
			etPressure.setText(sb.toString());
			break;			
			
		}
				
				
				
	}

}

  2.layout下的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	
    
<TextView
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="@string/accelerometer"
    	 />
     
<EditText
	android:id="@+id/etAccelerometer"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:editable="false"
	android:cursorVisible="false" />


<TextView
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="@string/magnetic" />
  <EditText
	android:id="@+id/etMagnetic"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:editable="false"
	android:cursorVisible="false" />
  

<TextView
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="@string/light" />
<EditText
	android:id="@+id/etLight"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:editable="false"
	android:cursorVisible="false" />	
	
	
<TextView
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="@string/pressure" />
<EditText
	android:id="@+id/etPressure"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:editable="false"
	android:cursorVisible="false" />			
</LinearLayout>

3. strings.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">SensorTest</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="accelerometer">速度传感器的值</string>
    <string name="magnetic">磁场传感器的值</string>
    <string name="light">光传感器的值</string>
     <string name="pressure">压力传感器的值</string>
            
</resources>

4.真机测试。





Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐