说到传感器,还是有很多的,有加速度啊,光照啊,磁传感器等等。当然android手机之所以称为智能手机,少不了这几款传感器的功劳了。下面就学习下了,这里主要学习光照,加速度和磁。
新建工程emSensorStudy,布局如下:
《?xml version=“1.0” encoding=“utf-8”?》
《LinearLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:orientation=“vertical”
android:layout_margin=“5dp”
tools:context=“com.jared.emsensorsstudy.MainActivity”》
《TextView
android:text=“Hello Sensors”
android:layout_gravity=“center”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:textSize=“22dp”/》
《Button
android:id=“@+id/startLightSensor”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:text=“启动LightSensor”
android:textAllCaps=“false”/》
《Button
android:id=“@+id/startAccelerSensor”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:text=“启动AccelerSensor”
android:textAllCaps=“false”/》
《Button
android:id=“@+id/startMagneticSensor”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:text=“启动MagneticSensor”
android:textAllCaps=“false”/》
《/LinearLayout》
添加LightSensor,AccelerSensor,MagnetiSensor的Activity,修改MainActivity代码如下:
package com.jared.emsensorsstudy;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button startLightSensorBtn;
private Button startAccelerSensorBtn;
private Button startMagneticSensorBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startLightSensorBtn = (Button)findViewById(R.id.startLightSensor);
startAccelerSensorBtn = (Button)findViewById(R.id.startAccelerSensor);
startMagneticSensorBtn = (Button)findViewById(R.id.startMagneticSensor);
startLightSensorBtn.setOnClickListener(new myOnClickListener());
startAccelerSensorBtn.setOnClickListener(new myOnClickListener());
startMagneticSensorBtn.setOnClickListener(new myOnClickListener());
}
private class myOnClickListener implements View.OnClickListener {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.startAccelerSensor:
Intent intent1 = new Intent(getApplicationContext(), AccelerSensor.class);
startActivity(intent1);
break;
case R.id.startLightSensor:
Intent intent2 = new Intent(getApplicationContext(), LightSensor.class);
startActivity(intent2);
break;
case R.id.startMagneticSensor:
Intent intent3 = new Intent(getApplicationContext(), MagneticSensor.class);
startActivity(intent3);
break;
default:
break;
}
}
}
}
先要实现Light的功能,先修改布局如下:
《?xml version=“1.0” encoding=“utf-8”?》
《LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:layout_margin=“10dp”
tools:context=“com.jared.emsensorsstudy.LightSensor”》
《TextView
android:id=“@+id/light_level”
android:layout_gravity=“center”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:textSize=“22dp”/》
《/LinearLayout》
简单地实现了一个textview用来显示光照强度。接着修改LightSensor代码如下:
package com.jared.emsensorsstudy;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class LightSensor extends AppCompatActivity {
private SensorManager sensorManager;
private TextView lightLevel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_light_sensor);
lightLevel = (TextView)findViewById(R.id.light_level);
initWithLight();
}
@Override
protected void onDestroy() {
super.onDestroy();
if(sensorManager != null) {
sensorManager.unregisterListener(listener);
}
}
public void initWithLight() {
sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}
private SensorEventListener listener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
float value = sensorEvent.values[0];
lightLevel.setText(“Currrent light level is ”+value+“lx”);
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
};