传感器应用开发
传感器开发基本代码流程:
onCreate():
SensorManager sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
Sensor sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
rate = SensorManager.SENSOR_DELAY_NORMAL;
onResume();
sm.registerListener(SensorEventListener listener,Sensor sensor, int rate);
onStop():
sm.unregisterListener(SensorEventListener listener);
传感器开发实例 代码片段:
package com.bob.mycrazy; import android.app.Activity; import android.app.Fragment; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; public class SensorFragment extends Fragment implements SensorEventListener { private EditText etorientation, etmagnetic, ettemperatura, etlight, etpresure; private static SensorManager sm;//sensor管理器 public static SensorFragment newInstance(SensorManager sensorManager) { SensorFragment fragment = new SensorFragment(); Bundle args = new Bundle(); fragment.setArguments(args); sm = sensorManager; return fragment; } public SensorFragment() { } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater .inflate(R.layout.fragment_sensor, container, false); etorientation = (EditText) view.findViewById(R.id.etorientation); etmagnetic = (EditText) view.findViewById(R.id.etmagnetic); ettemperatura = (EditText) view.findViewById(R.id.ettemperatura); etlight = (EditText) view.findViewById(R.id.etlight); etpresure = (EditText) view.findViewById(R.id.etpresure); return view; } @Override public void onResume() { //为方向传感器注册监听器 sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_NORMAL); //为磁场传感器注册监听器 sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_NORMAL); //为温度传感器注册监听器 sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE), SensorManager.SENSOR_DELAY_NORMAL); //为光传感器注册监听器 sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_LIGHT), SensorManager.SENSOR_DELAY_NORMAL); //为压力传感器注册监听器 sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_PRESSURE), SensorManager.SENSOR_DELAY_NORMAL); super.onResume(); } @Override public void onStop() { //退出时取消注册监听器 sm.unregisterListener(this); super.onStop(); } @Override public void onPause() { //暂停时取消注册监听器 sm.unregisterListener(this); super.onPause(); } @Override public void onAttach(Activity activity) { super.onAttach(activity); } //SensorEventListener接口必须实现的方法 @Override public void onSensorChanged(SensorEvent event) { //获取传感器返回的值 float[] values = event.values; //获取触发监听的传感器类型 int type = event.sensor.getType(); StringBuilder sb = new StringBuilder(); switch (type) { //方向传感器 case Sensor.TYPE_ORIENTATION: sb.append("绕Z轴转过的角度:" + values[0]); sb.append("\n绕x轴转过的角度:" + values[1]); sb.append("\n绕y轴转过的角度:" + values[2]); etorientation.setText(sb.toString()); break; //磁场传感器 case Sensor.TYPE_MAGNETIC_FIELD: sb.append("x方向上的角度:" + values[0]); sb.append("\ny方向上的角度:" + values[1]); sb.append("\nz方向上的角度:" + values[2]); etmagnetic.setText(sb.toString()); break; //温度传感器 case Sensor.TYPE_AMBIENT_TEMPERATURE: sb.append("当前温度为:" + values[0]); ettemperatura.setText(sb.toString()); break; //光传感器 case Sensor.TYPE_LIGHT: sb.append("当前光的强度为:" + values[0]); etlight.setText(sb.toString()); break; //压力传感器 case Sensor.TYPE_PRESSURE: sb.append("当前压力为:" + values[0]); etpresure.setText(sb.toString()); break; } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } }
布局效果:
相关推荐
quyunfei 2020-10-23
hxq 2020-09-27
suiling 2020-09-16
Duriansir 2020-07-16
dm00 2020-06-28
CSDN人工智能头条 2020-06-27
MuziWater 2020-06-22
Norsaa 2020-06-05
digihail0 2020-06-04
kuankeTech 2020-06-02
inabaraku 2020-05-22
yuancr 2020-05-10
zxznsjdsj 2020-04-22
seeyousoonhhh 2020-04-07
大数据周刊 2020-03-25
yuancr 2020-03-04
编码美丽 2020-01-11
internetofthings 2020-01-04
YingCloud 2019-12-27