cari

Rumah  >  Soal Jawab  >  teks badan

android 怎么获取手机里面所有的信息并显示出来

就是把手机里面多有的传感器数据获取到,然后显示在界面上?求大神解决。

高洛峰高洛峰2767 hari yang lalu507

membalas semua(1)saya akan balas

  • PHP中文网

    PHP中文网2017-04-17 15:07:08

    方向传感器: Sensor.TYPE_ORIENTATION
    加速度传感器: Sensor.TYPE_ACCELEROMETER
    光线传感器: Sensor.TYPE_LIGHT
    磁场传感器: Sensor.TYPE_MAGNETIC_FIELD
    距离传感器: Sensor.TYPE_PROXIMITY
    温度传感器: Sensor.TYPE_TEMPERATURE

    //获取某种类型的感应器
    Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

    //注册监听
    sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_GAME);

    上面第三个参数为采样率
    最快: SensorManager.SENSOR_DELAY_FASTEST
    游戏: SensorManager.SENSOR_DELAY_GAME
    普通: SensorManager.SENSOR_DELAY_NORMAL
    用户界面: SensorManager.SENSOR_DELAY_UI





    用获取加速传感器数据举例:

    //MainActivity.java
    package aaa.android.com.myapplication123;
    
    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 MainActivity extends AppCompatActivity {
        private TextView textView;
        private MySensorEventListener sensorEventListener = new MySensorEventListener();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            textView = (TextView) findViewById(R.id.text_view);
            
            //获取管理器
            SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    
            //获取加速度感应器 
            Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
            sensorManager.registerListener(sensorEventListener, sensor, SensorManager.SENSOR_DELAY_NORMAL); 
        }
    
        private final class MySensorEventListener implements SensorEventListener {
            //传感器实时变化值
            @Override
            public void onSensorChanged(SensorEvent event) {
                if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER) {
                    float x = event.values[0];
                    float y = event.values[1];
                    float z = event.values[2];
                    String result = "X= " + x + "\n"
                            + "Y= " + y + "\n"
                            + "Z= " + z;
                    textView.setText(result);
                }
            }
    
            @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy) {
    
            }
        }
    }
    
    
    //activity_main.xml
    <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:padding="30dp"
        tools:context="aaa.android.com.myapplication123.MainActivity">
        
        <!--定义一个textview用来显示结果-->
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="test"
            android:textSize="30sp"
            android:id="@+id/text_view"/>
    
    </LinearLayout>

    balas
    0
  • Batalbalas