安卓 2.3.3 API10以上) 读取传感器 加速度 磁场 最简单
一个最简单的demo,读取加速度传感器和磁场传感器,直接打印。适合业余玩玩。
用的技术点都是最简单粗暴的,不要用来开发产品。
在安卓2.3.3和5.1的真机上都测试通过。
安卓源码:使用的android studio开发,SDK是API10,姜饼。
为了在一个10年前的手机上玩,三星5830,竟然还能用。
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.po.mysensor.MainActivity"> <TextView android:id="@+id/po_acc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="po_acc_txt" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.502" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/po_meg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="po_meg_txt" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.502" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.251" /> </android.support.constraint.ConstraintLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.po.mysensor"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
MainActivity.java
package com.example.po.mysensor; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private TextView po_acc_txt, po_meg_txt; private SensorManager manager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); po_acc_txt = (TextView)findViewById(R.id.po_acc); po_meg_txt = (TextView)findViewById(R.id.po_meg); manager = (SensorManager) getSystemService(SENSOR_SERVICE); } @Override protected void onResume() { super.onResume(); manager.registerListener(listener_acc, manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_UI); manager.registerListener(listener_meg, manager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_UI); } private SensorEventListener listener_acc = new SensorEventListener() { @Override public void onSensorChanged(SensorEvent event) { if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { String tmp = "TYPE_ORIENTATION:x=" + event.values[0] + "TYPE_ORIENTATION:y=" + event.values[1]; po_acc_txt.setText(tmp); } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } }; private SensorEventListener listener_meg = new SensorEventListener() { @Override public void onSensorChanged(SensorEvent event) { if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) { String tmp = "TYPE_MAGNETIC_FIELD:x=" + event.values[0] + "TYPE_MAGNETIC_FIELD:y=" + event.values[1]; po_meg_txt.setText(tmp); } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } }; @Override protected void onStop() { manager.unregisterListener(listener_acc); manager.unregisterListener(listener_meg); super.onStop(); } }