Sensor Changed event and information : Sensor « Hardware « Android

Home
Android
1.2D Graphics
2.Animation
3.Core Class
4.Database
5.Date Type
6.Development
7.File
8.Game
9.Hardware
10.Media
11.Network
12.Security
13.UI
14.User Event
Android » Hardware » Sensor 
Sensor Changed event and information
   
package app.test;

import android.app.Activity;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

public class Test extends Activity implements SensorEventListener {
  private SensorManager mgr;
  private Sensor accel;
    private Sensor compass;
  private Sensor orient;
    private TextView preferred;
  private TextView orientation;
  private boolean ready = false;
  private float[] accelValues = new float[3];
  private float[] compassValues = new float[3];
  private float[] inR = new float[9];
  private float[] inclineMatrix = new float[9];
  private float[] orientationValues = new float[3];
  private float[] prefValues = new float[3];
  private float mAzimuth;
  private double mInclination;
  private int counter;
  private int mRotation;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        preferred = (TextView)findViewById(R.id.preferred);
        orientation = (TextView)findViewById(R.id.orientation);
        mgr = (SensorManagerthis.getSystemService(SENSOR_SERVICE);
        accel = mgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        compass = mgr.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
        orient = mgr.getDefaultSensor(Sensor.TYPE_ORIENTATION);
        WindowManager window = (WindowManagerthis.getSystemService(WINDOW_SERVICE);
        int apiLevel = Integer.parseInt(Build.VERSION.SDK);
        if(apiLevel < 8) {
            mRotation = window.getDefaultDisplay().getOrientation();
        }
        else {
          mRotation = window.getDefaultDisplay().getRotation();
        }
    }
    @Override
    protected void onResume() {
        mgr.registerListener(this, accel, SensorManager.SENSOR_DELAY_GAME);
        mgr.registerListener(this, compass, SensorManager.SENSOR_DELAY_GAME);
        mgr.registerListener(this, orient, SensorManager.SENSOR_DELAY_GAME);
      super.onResume();
    }
    @Override
    protected void onPause() {
        mgr.unregisterListener(this, accel);
        mgr.unregisterListener(this, compass);
        mgr.unregisterListener(this, orient);
      super.onPause();
    }
  public void onAccuracyChanged(Sensor sensor, int accuracy) {
  }
  public void onSensorChanged(SensorEvent event) {
    switch(event.sensor.getType()) {
    case Sensor.TYPE_ACCELEROMETER:
      for(int i=0; i<3; i++) {
              accelValues[i= event.values[i];
      }
            if(compassValues[0!= 0)
              ready = true;
            break;
    case Sensor.TYPE_MAGNETIC_FIELD:
      for(int i=0; i<3; i++) {
        compassValues[i= event.values[i];
      }
            if(accelValues[2!= 0)
              ready = true;
            break;
    case Sensor.TYPE_ORIENTATION:
      for(int i=0; i<3; i++) {
        orientationValues[i= event.values[i];
      }
        break;
    }    
        if(!ready)
          return;
        if(SensorManager.getRotationMatrix(
                inR, inclineMatrix, accelValues, compassValues)) {
          mInclination = SensorManager.getInclination(inclineMatrix);
          SensorManager.getOrientation(inR, prefValues);
          if(counter++ % 10 == 0) {
            doUpdate(null);
            counter = 1;
          }
        }
  }
    public void doUpdate(View view) {
      if(!ready)
        return;
      mAzimuth = (floatMath.toDegrees(prefValues[0]);
      if(mAzimuth < 0) {
        mAzimuth += 360.0f;
      }
        String msg = String.format(
            "Preferred:\nazimuth (Z): %7.3f \npitch (X): %7.3f\nroll (Y): %7.3f",
            mAzimuth, Math.toDegrees(prefValues[1]),
             Math.toDegrees(prefValues[2]));
      preferred.setText(msg);
        msg = String.format(
            "Orientation Sensor:\nazimuth (Z): %7.3f\npitch (X): %7.3f\nroll (Y): %7.3f",
            orientationValues[0],
            orientationValues[1],
             orientationValues[2]);
        orientation.setText(msg);
         preferred.invalidate();
         orientation.invalidate();
    }
    public void doShow(View view) {
        Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse(
            "google.streetview:cbll=30.32454,-81.6584&cbp=1," +
            Math.round(orientationValues[0]) ",,0,1.0"
            ));
      startActivity(intent);
      return;
    }
}

//main.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- This file is res/layout/main.xml -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"  android:layout_height="fill_parent"
    >
  <Button  android:id="@+id/update"  android:text="update"
    android:layout_width="wrap_content"  android:layout_height="wrap_content" 
    android:onClick="doUpdate" />
  <Button  android:id="@+id/show"  android:text="show"
    android:layout_width="wrap_content"  android:layout_height="wrap_content" 
    android:onClick="doShow" android:layout_toRightOf="@id/update" />
  <TextView  android:id="@+id/preferred" android:textSize="20sp"
    android:layout_width="wrap_content"  android:layout_height="wrap_content"
    android:layout_below="@id/update" />
  <TextView  android:id="@+id/orientation" android:textSize="20sp"
    android:layout_width="wrap_content"  android:layout_height="wrap_content"
    android:layout_below="@id/preferred" />
</RelativeLayout>

   
    
    
  
Related examples in the same category
1.Using Sensor
2.Accelero meter Sensor
3.Compass sensor
4.Gravity sensor
5.Sensor.TYPE_GYROSCOPE
6.Light Sensor
7.PROXIMITY Sensor
8.Sensor List
9.Temperature Sensor
10.Sensor Event Listener
11.Speed sensor
12.Sensor Test
13.Displays the values of the acceleration sensor graphically
14.Compass Activity
15.Accelerometer API
16.Using the accelerometer to integrate the device's acceleration to a position using the Verlet method.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.