Gravity sensor : 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 
Gravity sensor
   

package app.test;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class Test extends Activity implements SensorEventListener {
    private SensorManager mgr;
    private Sensor accelerometer;
    private TextView text;
  private float[] gravity = new float[3];
  private float[] motion = new float[3];
  private double ratio;
  private double mAngle;
  private int counter = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mgr = (SensorManagerthis.getSystemService(SENSOR_SERVICE);
        accelerometer = mgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        text = (TextViewfindViewById(R.id.text);
    }
    @Override
    protected void onResume() {
        mgr.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI);
      super.onResume();
    }

    @Override
    protected void onPause() {
        mgr.unregisterListener(this, accelerometer);
      super.onPause();
    }

  public void onAccuracyChanged(Sensor sensor, int accuracy) {
  }

  public void onSensorChanged(SensorEvent event) {
    for(int i=0; i<3; i++) {
            gravity [i(float) (0.1 * event.values[i0.9 * gravity[i]);
            motion[i= event.values[i- gravity[i];
    }
      ratio = gravity[1]/SensorManager.GRAVITY_EARTH;
      if(ratio > 1.0ratio = 1.0;
      if(ratio < -1.0ratio = -1.0;
      mAngle = Math.toDegrees(Math.acos(ratio));
      if(gravity[20) {
        mAngle = -mAngle;
      }
      if(counter++ % 10 == 0) {
            String msg = String.format(
                "Raw values\nX: %8.4f\nY: %8.4f\nZ: %8.4f\n" +
          "Gravity\nX: %8.4f\nY: %8.4f\nZ: %8.4f\n" +
          "Motion\nX: %8.4f\nY: %8.4f\nZ: %8.4f\nAngle: %8.1f",
              event.values[0], event.values[1], event.values[2],
              gravity[0], gravity[1], gravity[2],
              motion[0], motion[1], motion[2],
              mAngle);
        text.setText(msg);
        text.invalidate();
        counter=1;
      }
  }
}

//main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"  android:layout_height="fill_parent" >
  <TextView  android:id="@+id/text" android:textSize="20sp"
    android:layout_width="fill_parent"  android:layout_height="wrap_content" />
</LinearLayout>

   
    
    
  
Related examples in the same category
1.Using Sensor
2.Accelero meter Sensor
3.Compass sensor
4.Sensor.TYPE_GYROSCOPE
5.Light Sensor
6.PROXIMITY Sensor
7.Sensor List
8.Temperature Sensor
9.Sensor Changed event and information
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.