ulfben

Listing all sensors (Android)

Mar 7th, 2017
126
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. private SensorManager mSensorManager;
  2.  
  3. public void onCreate(Bundle savedInstanceState) {
  4.     super.onCreate(savedInstanceState);
  5.     setContentView(R.layout.main);
  6.    
  7.     // Get the texts fields of the layout and set to invisible
  8.     TextView sensorCountText =
  9.         (TextView) findViewById(R.id.sensorCountText);
  10.     TextView sensorNamesText =
  11.         (TextView) findViewById(R.id.sensorNamesText);
  12.    
  13.     // Get the SensorManager
  14.     mSensorManager =
  15.         (SensorManager) getSystemService(Context.SENSOR_SERVICE);
  16.    
  17.     // get a list of available sensors
  18.     List<Sensor> sensors =
  19.         mSensorManager.getSensorList(Sensor.TYPE_ALL);
  20.        
  21.     // Print how may Sensors there are
  22.     sensorCountText.setText(sensors.size() +" sensors available.");
  23.    
  24.     // Print the name of each Sensor
  25.     StringBuilder out = new StringBuilder();
  26.     for(Sensor s : sensors){       
  27.         out.append(s.getName()).append("\n");
  28.     }
  29.     sensorNamesText.setText(out.toString());
  30. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×