浅入浅出Android(007):看看你的手机上有哪些传感器

原创
2014/05/11 20:05
阅读数 506
请先参考 浅入浅出Android(006):实时获取加速计Accelerometer的信息

不多说,先上代码再配图。

1、建立android项目


2、修改布局layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >
    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="有哪些传感器?"
            />
    <TextView
            android:id="@+id/sensor_list"
            android:text=""
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />

</LinearLayout>



3、修改java代码


package com.example.HelloWorld;

import android.app.Activity;
import android.os.Bundle;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.widget.TextView;
import java.util.List;

public class MyActivity extends Activity{

    private SensorManager sensorManager;
    TextView sensorList;
    private String sensorsInMyPhone = "";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        sensorList=(TextView)findViewById(R.id.sensor_list);


        sensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);

        List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
        for (Sensor sensor : sensors) {
            sensorsInMyPhone = sensorsInMyPhone + sensor.getName() + "\n";
        }

        sensorList.setText(sensorsInMyPhone);

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}



4、效果图


展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
3 收藏
0
分享
返回顶部
顶部