說到傳感器,還是有很多的,有加速度啊,光照啊,磁傳感器等等。當然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) {
}
};