Feb 10, 2013

Get details of gravity sensor

To list installed gravity sensor, call getDefaultSensor() of SensorManager instance, with Sensor.TYPE_GRAVITY which introduced in API level 9. What it return is a List of Sensor, with details of the associated sensor.

package com.example.androidgravitysensor;

import java.util.List;

import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;

public class MainActivity extends ListActivity {
 
 private SensorManager mySensorManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        mySensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
        if (mySensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY) != null){
         List<Sensor> gravitySensorList = mySensorManager.getSensorList(Sensor.TYPE_GRAVITY);
         
         setListAdapter(new ArrayAdapter<Sensor>(
           this, 
           android.R.layout.simple_list_item_1, 
           gravitySensorList));
         
        }else{
         Toast.makeText(getApplicationContext(), "No Gravity Sensor!", Toast.LENGTH_LONG).show();
        }
    }

 @Override
 protected void onListItemClick(ListView l, View v, int position, long id) {
  Sensor sensor = (Sensor)l.getItemAtPosition(position);
  
  AlertDialog.Builder sensorDialog = new AlertDialog.Builder(MainActivity.this);
  sensorDialog.setTitle(sensor.getName());

  Class<Sensor> sensorClass = (Class<Sensor>)sensor.getClass();
  String vendor = sensor.getVendor();
  int version = sensor.getVersion();
  int type = sensor.getType();
  int minDelay = sensor.getMinDelay();
  float power = sensor.getPower();
  float maxRange = sensor.getMaximumRange();
  float resolution = sensor.getResolution();
  
  LinearLayout dialogLayout = new LinearLayout(getApplicationContext());
  LayoutParams dialogLayoutParams = new LayoutParams(
    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
  dialogLayout.setLayoutParams(dialogLayoutParams);
  dialogLayout.setOrientation(LinearLayout.VERTICAL);
  
  TextView textSensorClass = new TextView(getApplicationContext());
  textSensorClass.setText(sensorClass.toString());
  TextView textVendor = new TextView(getApplicationContext());
  textVendor.setText("Vendor: " + vendor);
  TextView textVersion = new TextView(getApplicationContext());
  textVersion.setText("Version: " + String.valueOf(version));
  TextView textType = new TextView(getApplicationContext());
  textType.setText("Type: " + String.valueOf(type));
  TextView textMinDelay = new TextView(getApplicationContext());
  textMinDelay.setText("MinDelay: " + String.valueOf(minDelay) + " microsecond");
  TextView textPower = new TextView(getApplicationContext());
  textPower.setText("Power: " + String.valueOf(power) + " mA");
  TextView textMaxRange = new TextView(getApplicationContext());
  textMaxRange.setText("MaximumRange: " + String.valueOf(maxRange));
  TextView textResolution = new TextView(getApplicationContext());
  textResolution.setText("Resolution: " + String.valueOf(resolution));
  
  dialogLayout.addView(textSensorClass);
  dialogLayout.addView(textVendor);
  dialogLayout.addView(textVersion);
  dialogLayout.addView(textType);
  dialogLayout.addView(textMinDelay);
  dialogLayout.addView(textPower);
  dialogLayout.addView(textMaxRange);
  dialogLayout.addView(textResolution);
  
  ScrollView scrollView = new ScrollView(getApplicationContext());
  LayoutParams scrollViewLayoutParams = new LayoutParams(
    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
  scrollView.setLayoutParams(scrollViewLayoutParams);
  scrollView.addView(dialogLayout);
       
  sensorDialog.setView(scrollView);
  sensorDialog.show();
 }

}


Get details of gravity sensor
details of gravity sensor


Related article: Detect device orientation by gravity sensor

No comments:

Post a Comment

Infolinks In Text Ads