Skip to content

Instantly share code, notes, and snippets.

@massimomusante
Created November 12, 2012 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save massimomusante/4059268 to your computer and use it in GitHub Desktop.
Save massimomusante/4059268 to your computer and use it in GitHub Desktop.
Android sensor explorer (fragments)
public class SensorsActivity extends Activity implements SensorEventListener, OnItemSelectedListener
{
...
}
...
// Sensors manager
private SensorManager sm = null;
// Some UI elements
private Spinner spin = null;
private TextView name = null;
private TextView vendor = null;
private TextView type = null;
private GridView results = null;
// List of discovered sensors
private List<Sensor> sensorsList;
private int selected = -1;
...
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sensors);
// get sensors manager
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
// get UI elements handlers
spin = (Spinner) findViewById(R.id.sensorsList);
name = (TextView) findViewById(R.id.nameValue);
type = (TextView) findViewById(R.id.typeValue);
vendor = (TextView) findViewById(R.id.vendorValue);
results = (GridView) findViewById(R.id.sensorValues);
// set spinner listener
spin.setOnItemSelectedListener(this);
// Fills the spinner
sensorsList = sm.getSensorList(Sensor.TYPE_ALL);
ArrayList<String> als = new ArrayList<String>();
for(Sensor s : sensorsList)
{
als.add(s.getType() + " : " + s.getName());
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, als);
spin.setAdapter(adapter);
}
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
// Unregister current
sm.unregisterListener(this);
// Register selected one
Sensor sensor = sensorsList.get(pos);
selected = pos;
sm.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
// write sensors details
name.setText(sensor.getName());
vendor.setText(sensor.getVendor());
type.setText(String.valueOf(sensor.getType()));
}
protected void onStop()
{
sm.unregisterListener(this);
super.onStop();
}
...
protected void onPause()
{
super.onPause();
sm.unregisterListener(this);
}
public void onSensorChanged(SensorEvent event)
{
String []vals = new String[event.values.length];
for(int j=0;j<vals.length;j++)
{
vals[j] = j + " : " + event.values[j];
}
// Writes sensor data to grid view
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, vals);
results.setAdapter(adapter);
}
<RelativeLayout 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:layout_margin="1dp"
android:gravity="top" >
<Spinner
android:id="@+id/sensorsList"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/nameLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/sensorsList"
android:text="@string/name" />
<TextView
android:id="@+id/vendorLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/nameLabel"
android:text="@string/vendor" />
<TextView
android:id="@+id/typeLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/vendorLabel"
android:text="@string/type" />
<TextView
android:id="@+id/nameValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/vendorLabel"
android:layout_alignParentRight="true"
android:text="------" />
<TextView
android:id="@+id/typeValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/vendorLabel"
android:text="------" />
<TextView
android:id="@+id/vendorValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/typeValue"
android:layout_alignParentRight="true"
android:text="------" />
<GridView
android:id="@+id/sensorValues"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/typeLabel"
android:layout_marginTop="27dp"
android:numColumns="1" >
</GridView>
</RelativeLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment