Display an Android Sensors List

后端 未结 4 1871
梦谈多话
梦谈多话 2021-02-01 18:46

I\'m trying to display a list of available sensors but it\'s like there are not!
I was thinking that it was because of the emulator, but i tried it on the phone and the re

4条回答
  •  闹比i
    闹比i (楼主)
    2021-02-01 19:41

    You can try this code:-

    package com.example.sensor;
    
    import java.util.List;
    
    import android.content.Context;
    import android.hardware.Sensor;
    import android.hardware.SensorManager;
    import android.os.Bundle;
    import android.support.v7.app.ActionBarActivity;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    
    public class MainActivity extends ActionBarActivity {
    
    SensorManager smm;
    List sensor;
    ListView lv;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        smm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        lv = (ListView) findViewById (R.id.listView1);
        sensor = smm.getSensorList(Sensor.TYPE_ALL);
        lv.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1,  sensor));
    }
    }
    

    Just put a list view in your xml layout.

提交回复
热议问题