Error with AccelerometerPlay Sample of the SDK


Last Updated: 2011-04-11 21:59:13
  1. lugopaco

    lugopaco Active Member

    Hi. I'm some kind of new at the Java language and to the ANDROID. I was checking the samples that comes with the SDK and that of the accelerometer caught particulary my attention, but once I tried to run it to see what it does, appeared that I've got 2 errors:

    -The method onAccuracyChanged(Sensor, int) of type AccelerometerPlayActivity.SimulationView must override a superclass method AccelerometerPlayActivity.java /AccelerometerPlay/src/com/example/android/accelerometerplay line 457 Java Problem

    -The method onSensorChanged(SensorEvent) of type AccelerometerPlayActivity.SimulationView must override a superclass method AccelerometerPlayActivity.java /AccelerometerPlay/src/com/example/android/accelerometerplay line 378 Java Problem

    And 1 warning:

    -Attribute minSdkVersion (5) is lower than the project target API level (10) AndroidManifest.xml /AccelerometerPlay line 1 Android ADT Problem

    I will really apreciate any kind of help!!!!!!!

    Advertisement
  2. lugopaco

    lugopaco Active Member

    The code is (I added "*********" at the end of the lines that causes the errors):

    Code (Text):
    1.  
    2. /*
    3.  * Copyright (C) 2010 The Android Open Source Project
    4.  *
    5.  * Licensed under the Apache License, Version 2.0 (the "License");
    6.  * you may not use this file except in compliance with the License.
    7.  * You may obtain a copy of the License at
    8.  *
    9.  *      [URL]http://www.apache.org/licenses/LICENSE-2.0[/URL]
    10.  *
    11.  * Unless required by applicable law or agreed to in writing, software
    12.  * distributed under the License is distributed on an "AS IS" BASIS,
    13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14.  * See the License for the specific language governing permissions and
    15.  * limitations under the License.
    16.  */
    17.  
    18. package com.example.android.accelerometerplay;
    19.  
    20. import android.app.Activity;
    21. import android.content.Context;
    22. import android.graphics.Bitmap;
    23. import android.graphics.BitmapFactory;
    24. import android.graphics.Canvas;
    25. import android.graphics.BitmapFactory.Options;
    26. import android.hardware.Sensor;
    27. import android.hardware.SensorEvent;
    28. import android.hardware.SensorEventListener;
    29. import android.hardware.SensorManager;
    30. import android.os.Bundle;
    31. import android.os.PowerManager;
    32. import android.os.PowerManager.WakeLock;
    33. import android.util.DisplayMetrics;
    34. import android.view.Display;
    35. import android.view.Surface;
    36. import android.view.View;
    37. import android.view.WindowManager;
    38.  
    39. /**
    40.  * This is an example of using the accelerometer to integrate the device's
    41.  * acceleration to a position using the Verlet method. This is illustrated with
    42.  * a very simple particle system comprised of a few iron balls freely moving on
    43.  * an inclined wooden table. The inclination of the virtual table is controlled
    44.  * by the device's accelerometer.
    45.  *
    46.  * @see SensorManager
    47.  * @see SensorEvent
    48.  * @see Sensor
    49.  */
    50.  
    51. public class AccelerometerPlayActivity extends Activity {
    52.  
    53.     private SimulationView mSimulationView;
    54.     private SensorManager mSensorManager;
    55.     private PowerManager mPowerManager;
    56.     private WindowManager mWindowManager;
    57.     private Display mDisplay;
    58.     private WakeLock mWakeLock;
    59.  
    60.     /** Called when the activity is first created. */
    61.     @Override
    62.     public void onCreate(Bundle savedInstanceState) {
    63.         super.onCreate(savedInstanceState);
    64.  
    65.         // Get an instance of the SensorManager
    66.         mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    67.  
    68.         // Get an instance of the PowerManager
    69.         mPowerManager = (PowerManager) getSystemService(POWER_SERVICE);
    70.  
    71.         // Get an instance of the WindowManager
    72.         mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    73.         mDisplay = mWindowManager.getDefaultDisplay();
    74.  
    75.         // Create a bright wake lock
    76.         mWakeLock = mPowerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, getClass()
    77.                 .getName());
    78.  
    79.         // instantiate our simulation view and set it as the activity's content
    80.         mSimulationView = new SimulationView(this);
    81.         setContentView(mSimulationView);
    82.     }
    83.  
    84.     @Override
    85.     protected void onResume() {
    86.         super.onResume();
    87.         /*
    88.          * when the activity is resumed, we acquire a wake-lock so that the
    89.          * screen stays on, since the user will likely not be fiddling with the
    90.          * screen or buttons.
    91.          */
    92.         mWakeLock.acquire();
    93.  
    94.         // Start the simulation
    95.         mSimulationView.startSimulation();
    96.     }
    97.  
    98.     @Override
    99.     protected void onPause() {
    100.         super.onPause();
    101.         /*
    102.          * When the activity is paused, we make sure to stop the simulation,
    103.          * release our sensor resources and wake locks
    104.          */
    105.  
    106.         // Stop the simulation
    107.         mSimulationView.stopSimulation();
    108.  
    109.         // and release our wake-lock
    110.         mWakeLock.release();
    111.     }
    112.  
    113.     class SimulationView extends View implements SensorEventListener {
    114.         // diameter of the balls in meters
    115.         private static final float sBallDiameter = 0.004f;
    116.         private static final float sBallDiameter2 = sBallDiameter * sBallDiameter;
    117.  
    118.         // friction of the virtual table and air
    119.         private static final float sFriction = 0.1f;
    120.  
    121.         private Sensor mAccelerometer;
    122.         private long mLastT;
    123.         private float mLastDeltaT;
    124.  
    125.         private float mXDpi;
    126.         private float mYDpi;
    127.         private float mMetersToPixelsX;
    128.         private float mMetersToPixelsY;
    129.         private Bitmap mBitmap;
    130.         private Bitmap mWood;
    131.         private float mXOrigin;
    132.         private float mYOrigin;
    133.         private float mSensorX;
    134.         private float mSensorY;
    135.         private long mSensorTimeStamp;
    136.         private long mCpuTimeStamp;
    137.         private float mHorizontalBound;
    138.         private float mVerticalBound;
    139.         private final ParticleSystem mParticleSystem = new ParticleSystem();
    140.  
    141.         /*
    142.          * Each of our particle holds its previous and current position, its
    143.          * acceleration. for added realism each particle has its own friction
    144.          * coefficient.
    145.          */
    146.         class Particle {
    147.             private float mPosX;
    148.             private float mPosY;
    149.             private float mAccelX;
    150.             private float mAccelY;
    151.             private float mLastPosX;
    152.             private float mLastPosY;
    153.             private float mOneMinusFriction;
    154.  
    155.             Particle() {
    156.                 // make each particle a bit different by randomizing its
    157.                 // coefficient of friction
    158.                 final float r = ((float) Math.random() - 0.5f) * 0.2f;
    159.                 mOneMinusFriction = 1.0f - sFriction + r;
    160.             }
    161.  
    162.             public void computePhysics(float sx, float sy, float dT, float dTC) {
    163.                 // Force of gravity applied to our virtual object
    164.                 final float m = 1000.0f; // mass of our virtual object
    165.                 final float gx = -sx * m;
    166.                 final float gy = -sy * m;
    167.  
    168.                 /*
    169.                  * &#65533;F = mA <=> A = &#65533;F / m We could simplify the code by
    170.                  * completely eliminating "m" (the mass) from all the equations,
    171.                  * but it would hide the concepts from this sample code.
    172.                  */
    173.                 final float invm = 1.0f / m;
    174.                 final float ax = gx * invm;
    175.                 final float ay = gy * invm;
    176.  
    177.                 /*
    178.                  * Time-corrected Verlet integration The position Verlet
    179.                  * integrator is defined as x(t+&#65533;t) = x(t) + x(t) - x(t-&#65533;t) +
    180.                  * a(t)&#65533;t&#65533;2 However, the above equation doesn't handle variable
    181.                  * &#65533;t very well, a time-corrected version is needed: x(t+&#65533;t) =
    182.                  * x(t) + (x(t) - x(t-&#65533;t)) * (&#65533;t/&#65533;t_prev) + a(t)&#65533;t&#65533;2 We also add
    183.                  * a simple friction term (f) to the equation: x(t+&#65533;t) = x(t) +
    184.                  * (1-f) * (x(t) - x(t-&#65533;t)) * (&#65533;t/&#65533;t_prev) + a(t)&#65533;t&#65533;2
    185.                  */
    186.                 final float dTdT = dT * dT;
    187.                 final float x = mPosX + mOneMinusFriction * dTC * (mPosX - mLastPosX) + mAccelX
    188.                         * dTdT;
    189.                 final float y = mPosY + mOneMinusFriction * dTC * (mPosY - mLastPosY) + mAccelY
    190.                         * dTdT;
    191.                 mLastPosX = mPosX;
    192.                 mLastPosY = mPosY;
    193.                 mPosX = x;
    194.                 mPosY = y;
    195.                 mAccelX = ax;
    196.                 mAccelY = ay;
    197.             }
    198.  
    199.             /*
    200.              * Resolving constraints and collisions with the Verlet integrator
    201.              * can be very simple, we simply need to move a colliding or
    202.              * constrained particle in such way that the constraint is
    203.              * satisfied.
    204.              */
    205.             public void resolveCollisionWithBounds() {
    206.                 final float xmax = mHorizontalBound;
    207.                 final float ymax = mVerticalBound;
    208.                 final float x = mPosX;
    209.                 final float y = mPosY;
    210.                 if (x > xmax) {
    211.                     mPosX = xmax;
    212.                 } else if (x < -xmax) {
    213.                     mPosX = -xmax;
    214.                 }
    215.                 if (y > ymax) {
    216.                     mPosY = ymax;
    217.                 } else if (y < -ymax) {
    218.                     mPosY = -ymax;
    219.                 }
    220.             }
    221.         }
    222.  
    223.         /*
    224.          * A particle system is just a collection of particles
    225.          */
    226.         class ParticleSystem {
    227.             static final int NUM_PARTICLES = 15;
    228.             private Particle mBalls[] = new Particle[NUM_PARTICLES];
    229.  
    230.             ParticleSystem() {
    231.                 /*
    232.                  * Initially our particles have no speed or acceleration
    233.                  */
    234.                 for (int i = 0; i < mBalls.length; i++) {
    235.                     mBalls[i] = new Particle();
    236.                 }
    237.             }
    238.  
    239.             /*
    240.              * Update the position of each particle in the system using the
    241.              * Verlet integrator.
    242.              */
    243.             private void updatePositions(float sx, float sy, long timestamp) {
    244.                 final long t = timestamp;
    245.                 if (mLastT != 0) {
    246.                     final float dT = (float) (t - mLastT) * (1.0f / 1000000000.0f);
    247.                     if (mLastDeltaT != 0) {
    248.                         final float dTC = dT / mLastDeltaT;
    249.                         final int count = mBalls.length;
    250.                         for (int i = 0; i < count; i++) {
    251.                             Particle ball = mBalls[i];
    252.                             ball.computePhysics(sx, sy, dT, dTC);
    253.                         }
    254.                     }
    255.                     mLastDeltaT = dT;
    256.                 }
    257.                 mLastT = t;
    258.             }
    259.  
    260.             /*
    261.              * Performs one iteration of the simulation. First updating the
    262.              * position of all the particles and resolving the constraints and
    263.              * collisions.
    264.              */
    265.             public void update(float sx, float sy, long now) {
    266.                 // update the system's positions
    267.                 updatePositions(sx, sy, now);
    268.  
    269.                 // We do no more than a limited number of iterations
    270.                 final int NUM_MAX_ITERATIONS = 10;
    271.  
    272.                 /*
    273.                  * Resolve collisions, each particle is tested against every
    274.                  * other particle for collision. If a collision is detected the
    275.                  * particle is moved away using a virtual spring of infinite
    276.                  * stiffness.
    277.                  */
    278.                 boolean more = true;
    279.                 final int count = mBalls.length;
    280.                 for (int k = 0; k < NUM_MAX_ITERATIONS && more; k++) {
    281.                     more = false;
    282.                     for (int i = 0; i < count; i++) {
    283.                         Particle curr = mBalls[i];
    284.                         for (int j = i + 1; j < count; j++) {
    285.                             Particle ball = mBalls[j];
    286.                             float dx = ball.mPosX - curr.mPosX;
    287.                             float dy = ball.mPosY - curr.mPosY;
    288.                             float dd = dx * dx + dy * dy;
    289.                             // Check for collisions
    290.                             if (dd <= sBallDiameter2) {
    291.                                 /*
    292.                                  * add a little bit of entropy, after nothing is
    293.                                  * perfect in the universe.
    294.                                  */
    295.                                 dx += ((float) Math.random() - 0.5f) * 0.0001f;
    296.                                 dy += ((float) Math.random() - 0.5f) * 0.0001f;
    297.                                 dd = dx * dx + dy * dy;
    298.                                 // simulate the spring
    299.                                 final float d = (float) Math.sqrt(dd);
    300.                                 final float c = (0.5f * (sBallDiameter - d)) / d;
    301.                                 curr.mPosX -= dx * c;
    302.                                 curr.mPosY -= dy * c;
    303.                                 ball.mPosX += dx * c;
    304.                                 ball.mPosY += dy * c;
    305.                                 more = true;
    306.                             }
    307.                         }
    308.                         /*
    309.                          * Finally make sure the particle doesn't intersects
    310.                          * with the walls.
    311.                          */
    312.                         curr.resolveCollisionWithBounds();
    313.                     }
    314.                 }
    315.             }
    316.  
    317.             public int getParticleCount() {
    318.                 return mBalls.length;
    319.             }
    320.  
    321.             public float getPosX(int i) {
    322.                 return mBalls[i].mPosX;
    323.             }
    324.  
    325.             public float getPosY(int i) {
    326.                 return mBalls[i].mPosY;
    327.             }
    328.         }
    329.  
    330.         public void startSimulation() {
    331.             /*
    332.              * It is not necessary to get accelerometer events at a very high
    333.              * rate, by using a slower rate (SENSOR_DELAY_UI), we get an
    334.              * automatic low-pass filter, which "extracts" the gravity component
    335.              * of the acceleration. As an added benefit, we use less power and
    336.              * CPU resources.
    337.              */
    338.             mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_UI);
    339.         }
    340.  
    341.         public void stopSimulation() {
    342.             mSensorManager.unregisterListener(this);
    343.         }
    344.  
    345.         public SimulationView(Context context) {
    346.             super(context);
    347.             mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    348.  
    349.             DisplayMetrics metrics = new DisplayMetrics();
    350.             getWindowManager().getDefaultDisplay().getMetrics(metrics);
    351.             mXDpi = metrics.xdpi;
    352.             mYDpi = metrics.ydpi;
    353.             mMetersToPixelsX = mXDpi / 0.0254f;
    354.             mMetersToPixelsY = mYDpi / 0.0254f;
    355.  
    356.             // rescale the ball so it's about 0.5 cm on screen
    357.             Bitmap ball = BitmapFactory.decodeResource(getResources(), R.drawable.ball);
    358.             final int dstWidth = (int) (sBallDiameter * mMetersToPixelsX + 0.5f);
    359.             final int dstHeight = (int) (sBallDiameter * mMetersToPixelsY + 0.5f);
    360.             mBitmap = Bitmap.createScaledBitmap(ball, dstWidth, dstHeight, true);
    361.  
    362.             Options opts = new Options();
    363.             opts.inDither = true;
    364.             opts.inPreferredConfig = Bitmap.Config.RGB_565;
    365.             mWood = BitmapFactory.decodeResource(getResources(), R.drawable.wood, opts);
    366.         }
    367.  
    368.         @Override
    369.         protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    370.             // compute the origin of the screen relative to the origin of
    371.             // the bitmap
    372.             mXOrigin = (w - mBitmap.getWidth()) * 0.5f;
    373.             mYOrigin = (h - mBitmap.getHeight()) * 0.5f;
    374.             mHorizontalBound = ((w / mMetersToPixelsX - sBallDiameter) * 0.5f);
    375.             mVerticalBound = ((h / mMetersToPixelsY - sBallDiameter) * 0.5f);
    376.         }
    377.  
    378.         @Override
    379.         public void onSensorChanged(SensorEvent event) { "**********"
    380.             if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
    381.                 return;
    382.             /*
    383.              * record the accelerometer data, the event's timestamp as well as
    384.              * the current time. The latter is needed so we can calculate the
    385.              * "present" time during rendering. In this application, we need to
    386.              * take into account how the screen is rotated with respect to the
    387.              * sensors (which always return data in a coordinate space aligned
    388.              * to with the screen in its native orientation).
    389.              */
    390.  
    391.             switch (mDisplay.getRotation()) {
    392.                 case Surface.ROTATION_0:
    393.                     mSensorX = event.values[0];
    394.                     mSensorY = event.values[1];
    395.                     break;
    396.                 case Surface.ROTATION_90:
    397.                     mSensorX = -event.values[1];
    398.                     mSensorY = event.values[0];
    399.                     break;
    400.                 case Surface.ROTATION_180:
    401.                     mSensorX = -event.values[0];
    402.                     mSensorY = -event.values[1];
    403.                     break;
    404.                 case Surface.ROTATION_270:
    405.                     mSensorX = event.values[1];
    406.                     mSensorY = -event.values[0];
    407.                     break;
    408.             }
    409.  
    410.             mSensorTimeStamp = event.timestamp;
    411.             mCpuTimeStamp = System.nanoTime();
    412.         }
    413.  
    414.         @Override
    415.         protected void onDraw(Canvas canvas) {
    416.  
    417.             /*
    418.              * draw the background
    419.              */
    420.  
    421.             canvas.drawBitmap(mWood, 0, 0, null);
    422.  
    423.             /*
    424.              * compute the new position of our object, based on accelerometer
    425.              * data and present time.
    426.              */
    427.  
    428.             final ParticleSystem particleSystem = mParticleSystem;
    429.             final long now = mSensorTimeStamp + (System.nanoTime() - mCpuTimeStamp);
    430.             final float sx = mSensorX;
    431.             final float sy = mSensorY;
    432.  
    433.             particleSystem.update(sx, sy, now);
    434.  
    435.             final float xc = mXOrigin;
    436.             final float yc = mYOrigin;
    437.             final float xs = mMetersToPixelsX;
    438.             final float ys = mMetersToPixelsY;
    439.             final Bitmap bitmap = mBitmap;
    440.             final int count = particleSystem.getParticleCount();
    441.             for (int i = 0; i < count; i++) {
    442.                 /*
    443.                  * We transform the canvas so that the coordinate system matches
    444.                  * the sensors coordinate system with the origin in the center
    445.                  * of the screen and the unit is the meter.
    446.                  */
    447.  
    448.                 final float x = xc + particleSystem.getPosX(i) * xs;
    449.                 final float y = yc - particleSystem.getPosY(i) * ys;
    450.                 canvas.drawBitmap(bitmap, x, y, null);
    451.             }
    452.  
    453.             // and make sure to redraw asap
    454.             invalidate();
    455.         }
    456.  
    457.         @Override
    458.         public void onAccuracyChanged(Sensor sensor, int accuracy) {"**********"
    459.         }
    460.     }
    461. }
    462.  
    463.  
  3. cr5315

    cr5315 Well-Known Member Developer

    Application Requests is not the right forum to be asking this. Try Application Development or Developer 101, that's where you'll be able to get help.
    lugopaco likes this.
  4. lugopaco

    lugopaco Active Member

    Thanks, I will try it.
  5. JiMMaR

    JiMMaR Well-Known Member

    urgh .. use CODE tag next time >.<
    anyway , that tutorial is old so it won't work [deprecated functions]
    I found this code on the internet , but I cannot find the site where I got it from , it worked fine for me
    using package name net.accTest and class name "accelerometerTest.java"

    Code (Text):
    1.  
    2. package net.accTest;
    3.  
    4.  
    5. import java.util.List;
    6.  
    7. import android.app.Activity;
    8. import android.content.Context;
    9. import android.os.Bundle;
    10. import android.hardware.Sensor;
    11. import android.hardware.SensorEvent;
    12. import android.hardware.SensorEventListener;
    13. import android.hardware.SensorManager;
    14. import android.view.View;
    15. import android.widget.Button;
    16. import android.widget.TextView;
    17.  
    18. public class accelerometerTest extends Activity {
    19.    
    20.     private TextView accText;
    21.     private SensorManager myManager;
    22.     private List<Sensor> sensors;
    23.     private Sensor accSensor;
    24.     private float oldX, oldY, oldZ = 0f,steps = 0f;
    25.    
    26.     /** Called when the activity is first created. */
    27.     @Override
    28.     public void onCreate(Bundle savedInstanceState) {
    29.         super.onCreate(savedInstanceState);        
    30.              
    31.         setContentView(R.layout.main);
    32.        
    33.         accText = (TextView)findViewById(R.id.tv);  
    34.         reset_btn.setOnClickListener(new View.OnClickListener() {
    35.            
    36.         // Set Sensor + Manager
    37.         myManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
    38.         sensors = myManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
    39.         if(sensors.size() > 0)
    40.         {
    41.             accSensor = sensors.get(0);
    42.         }
    43.        
    44.        
    45.     }
    46.  
    47.     private void updateTV(float x, float y, float z)
    48.     {
    49.         float thisX = x - oldX * 10;
    50.         float thisY = y - oldY * 10;
    51.         float thisZ = z - oldZ * 10;
    52.        
    53.         float nsteps = thisX*thisX +thisY*thisY +thisZ*thisZ;
    54.         if(nsteps >=2 )
    55.             steps++;
    56.        
    57.         accText.setText("x: " + Math.round(thisX) + ";\n y:" + Math.round(thisY) + ";\n z: " + Math.round(thisZ)+"\n steps Count: "+steps);
    58.        
    59.         oldX = x;
    60.         oldY = y;
    61.         oldZ = z;
    62.     }
    63.    
    64.     private final SensorEventListener mySensorListener = new SensorEventListener()
    65.     {
    66.         public void onSensorChanged(SensorEvent event)
    67.         {
    68.             updateTV(event.values[0],
    69.                     event.values[1],
    70.                     event.values[2]);
    71.         }
    72.        
    73.         public void onAccuracyChanged(Sensor sensor, int accuracy) {}
    74.     };
    75.    
    76.     @Override
    77.     protected void onResume()
    78.     {
    79.         super.onResume();
    80.         myManager.registerListener(mySensorListener, accSensor, SensorManager.SENSOR_DELAY_GAME);    
    81.     }
    82.    
    83.     @Override
    84.     protected void onStop()
    85.     {        
    86.         myManager.unregisterListener(mySensorListener);
    87.         super.onStop();
    88.     }
    89. }
    90.  
    and this will be your main.xml

    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    4.     android:orientation="vertical"
    5.     android:layout_width="fill_parent"
    6.     android:layout_height="fill_parent"
    7.     >
    8. <TextView  
    9.    
    10.     android:layout_width="fill_parent"
    11.     android:layout_height="wrap_content"
    12.     android:text="@string/hello"
    13.     android:id="@+id/tv"/>
    14. </LinearLayout>
    15.  
    I tested it on 2.1 update 1 sdk , tell me if you had any problems gettin it work

    @lugopaco you can edit your post and add <CODE> and </CODE> around your codes using [ and ] instead of < and > to rearrange it .. please do

    @MOD , could you please move this thread to the proper forum and delete the other duplicate threads
    lugopaco likes this.
  6. lugopaco

    lugopaco Active Member

    So many thanks!!!! That was exactly what I needed to know. I will try it later and I will write how is it working. Thanks so much for being so patient with me. I'm new at this kind of things such as forums and android.
  7. JiMMaR

    JiMMaR Well-Known Member

    no problem , come again ^_~*
    lugopaco likes this.
  8. lugopaco

    lugopaco Active Member

    I think main.xml code is missing. There are some errors if I run it that way. Dou you have that code JiMMaR???

    Also I found this code at internet, anyone could need it so:

    IBMEyes.java:

    Code (Text):
    1.  
    2. /*
    3.  *
    4.  * IBMEyes.java
    5.  * sample code for IBM Developerworks Article
    6.  * Author: W. Frank Ableson
    7.  * fableson@msiservices.com
    8.  *
    9.  */
    10.  
    11. package com.msi.ibm.eyes;
    12.  
    13. import android.app.Activity;
    14. import android.os.Bundle;
    15. import android.util.Log;
    16. import android.widget.TextView;
    17. import android.hardware.SensorManager;
    18. import android.hardware.SensorListener;
    19.  
    20. @SuppressWarnings("deprecation")
    21. public class IBMEyes extends Activity implements SensorListener {
    22.    
    23.     final String tag = "IBMEyes";
    24.     SensorManager sm = null;
    25.    
    26.     TextView xViewA = null;
    27.     TextView yViewA = null;
    28.     TextView zViewA = null;
    29.     TextView xViewO = null;
    30.     TextView yViewO = null;
    31.     TextView zViewO = null;
    32.    
    33.     /** Called when the activity is first created. */
    34.     @Override
    35.     public void onCreate(Bundle savedInstanceState) {
    36.         super.onCreate(savedInstanceState);
    37.         sm = (SensorManager) getSystemService(SENSOR_SERVICE);
    38.         setContentView(R.layout.main);
    39.         xViewA = (TextView) findViewById(R.id.xbox);
    40.         yViewA = (TextView) findViewById(R.id.ybox);
    41.         zViewA = (TextView) findViewById(R.id.zbox);
    42.         xViewO = (TextView) findViewById(R.id.xboxo);
    43.         yViewO = (TextView) findViewById(R.id.yboxo);
    44.         zViewO = (TextView) findViewById(R.id.zboxo);
    45.        
    46.     }
    47.    
    48.    
    49.    
    50.     public void onSensorChanged(int sensor, float[] values) {
    51.         synchronized (this) {
    52.             Log.d(tag, "onSensorChanged: " + sensor + ", x: " + values[0] + ", y: " + values[1] + ", z: " + values[2]);
    53.             if (sensor == SensorManager.SENSOR_ORIENTATION) {
    54.                 xViewO.setText("Orientation X: " + values[0]);
    55.                 yViewO.setText("Orientation Y: " + values[1]);
    56.                 zViewO.setText("Orientation Z: " + values[2]);
    57.             }
    58.             if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
    59.                 xViewA.setText("Accel X: " + values[0]);
    60.                 yViewA.setText("Accel Y: " + values[1]);
    61.                 zViewA.setText("Accel Z: " + values[2]);
    62.             }            
    63.         }
    64.     }
    65.    
    66.     public void onAccuracyChanged(int sensor, int accuracy) {
    67.         Log.d(tag,"onAccuracyChanged: " + sensor + ", accuracy: " + accuracy);
    68.        
    69.     }
    70.  
    71.  
    72.     @Override
    73.     protected void onResume() {
    74.         super.onResume();
    75.         sm.registerListener(this,
    76.                 SensorManager.SENSOR_ORIENTATION |
    77.                 SensorManager.SENSOR_ACCELEROMETER,
    78.                 SensorManager.SENSOR_DELAY_NORMAL);
    79.     }
    80.    
    81.     @Override
    82.     protected void onStop() {
    83.         sm.unregisterListener(this);
    84.         super.onStop();
    85.     }    
    86.    
    87.    
    88. }
    89.  
    main.xml:

    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    4.     android:orientation="vertical"
    5.     android:layout_width="fill_parent"
    6.     android:layout_height="fill_parent"
    7.     >
    8. <TextView  
    9.     android:layout_width="fill_parent"
    10.     android:layout_height="wrap_content"
    11.     android:text="@string/hello"
    12.     />
    13. <TextView  
    14.     android:layout_width="fill_parent"
    15.     android:layout_height="wrap_content"
    16.     android:text="Accelerometer"
    17.     />
    18. <TextView  
    19.     android:layout_width="fill_parent"
    20.     android:layout_height="wrap_content"
    21.     android:text="X Value"
    22.     android:id="@+id/xbox"
    23.     />
    24. <TextView  
    25.     android:layout_width="fill_parent"
    26.     android:layout_height="wrap_content"
    27.     android:text="Y Value"
    28.     android:id="@+id/ybox"
    29.     />
    30. <TextView  
    31.     android:layout_width="fill_parent"
    32.     android:layout_height="wrap_content"
    33.     android:text="Z Value"
    34.     android:id="@+id/zbox"
    35.     />    
    36.  
    37.  
    38. <TextView  
    39.     android:layout_width="fill_parent"
    40.     android:layout_height="wrap_content"
    41.     android:text="Orientation"
    42.     />
    43. <TextView  
    44.     android:layout_width="fill_parent"
    45.     android:layout_height="wrap_content"
    46.     android:text="X Value"
    47.     android:id="@+id/xboxo"
    48.     />
    49. <TextView  
    50.     android:layout_width="fill_parent"
    51.     android:layout_height="wrap_content"
    52.     android:text="Y Value"
    53.     android:id="@+id/yboxo"
    54.     />
    55. <TextView  
    56.     android:layout_width="fill_parent"
    57.     android:layout_height="wrap_content"
    58.     android:text="Z Value"
    59.     android:id="@+id/zboxo"
    60.     />    
    61.  
    62. </LinearLayout>
    63.  
    and AndroidManifest.xml:

    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    4.       package="com.msi.ibm.eyes"
    5.       android:versionCode="1"
    6.       android:versionName="1.0.0">
    7.     <application android:icon="@drawable/icon" android:label="@string/app_name">
    8.         <activity android:name=".IBMEyes"
    9.                   android:label="@string/app_name">
    10.             <intent-filter>
    11.                 <action android:name="android.intent.action.MAIN" />
    12.                 <category android:name="android.intent.category.LAUNCHER" />
    13.             </intent-filter>
    14.         </activity>
    15.     </application>
    16. </manifest>
    17.  
  9. JiMMaR

    JiMMaR Well-Known Member

    weird , it works fine for me
    lugopaco likes this.

Share This Page