Android传感器(一)-列出手机所具有的传感器

package com.xzs.sensor_test1;

import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private TextView textView;//用于列出手机上拥有的传感器
    private SensorManager sensorManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView=(TextView)findViewById(R.id.tv);
        
        sensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);
        List sensors=sensorManager.getSensorList(Sensor.TYPE_ALL);
        for(Sensor sensor:sensors){
            textView.append(sensor.getName()+"\n");
        }
    }
}

你可能感兴趣的:(Android)