说到传感器。还是有非常多的,有加速度啊,光照啊。磁传感器等等。当然android手机之所以称为智能手机,少不了这几款传感器的功劳了。以下就学习下了。这里主要学习光照,加速度和磁。
新建projectemSensorStudy,布局例如以下:
<?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) { } }; }
这里先通过getSystemService获取sensor,然后通过注冊一个listener来监听传感器的变化,当值有变化的时候会调用onSensorChanged方法,详细执行后。用手遮挡听筒附近的传感器,显演示样例如以下: