0

hi here is my code which i take from the link Moving an image using Accelerometer of android

package com.emblem.accelerometer;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;

public class AccelerometerActivity extends Activity implements
    SensorEventListener {
/** Called when the activity is first created. */
CustomDrawableView mCustomDrawableView = null;
ShapeDrawable mDrawable = new ShapeDrawable();
public static int x;
public static int y;

private SensorManager sensorManager = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    // Get a reference to a SensorManager
    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    mCustomDrawableView = new CustomDrawableView(this);
    setContentView(mCustomDrawableView);
    // setContentView(R.layout.main);

}

// This method will update the UI on new sensor events
public void onSensorChanged(SensorEvent sensorEvent) {

    if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        // the values you were calculating originally here were over
        // 10000!
        x = (int) Math.pow(sensorEvent.values[0], 2);
        y = (int) Math.pow(sensorEvent.values[1], 2);

    }

    if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) {

    }

}

// I've chosen to not implement this method
public void onAccuracyChanged(Sensor arg0, int arg1) {
    // TODO Auto-generated method stub

}

@Override
protected void onResume() {
    super.onResume();
    // Register this class as a listener for the accelerometer sensor
    sensorManager.registerListener(this,
            sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
            SensorManager.SENSOR_DELAY_NORMAL);
    // ...and the orientation sensor
    sensorManager.registerListener(this,
            sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
            SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
protected void onStop() {
    // Unregister the listener
    sensorManager.unregisterListener(this);
    super.onStop();
}

public class CustomDrawableView extends View {
    static final int width = 50;
    static final int height = 50;

    public CustomDrawableView(Context context) {
        super(context);

        mDrawable = new ShapeDrawable(new OvalShape());
        mDrawable.getPaint().setColor(0xff74AC23);
        mDrawable.setBounds(x, y, x + width, y + height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        RectF oval = new RectF(AccelerometerActivity.x,
                AccelerometerActivity.y, AccelerometerActivity.x + width,
                AccelerometerActivity.y + height); // set bounds of
                                                    // rectangle
        Paint p = new Paint(); // set some paint options
        p.setColor(Color.BLUE);
        canvas.drawOval(oval, p);
        invalidate();
    }
}
}

but activity shows nothing just a blank screen and also hangs . can any one help whats wrong ? Thanks

Community
  • 1
  • 1
Mudasar
  • 249
  • 1
  • 5
  • 15
  • I think that you should provide logcat. In addition, maybe you test your application on emulator? – Yury Jan 10 '12 at 09:45
  • yes i run it on emulater .. yury can you modify above code for me ? add a background thread which sleeps for 100 ms and then awake and check the accelerometer and then sleep – Mudasar Jan 10 '12 at 10:27
  • I think that you should do this by yourself and if you get a particular error you can ask us. This is how this service works. In the emulator you do not have accelerometer so I'm not surprised that you get an error. You should test this functionality on a real device. – Yury Jan 10 '12 at 17:10
  • yuvry there is default delay state for accelerometer instead of that i have to make thread for it and delay it for some seconds ? – Mudasar Jan 11 '12 at 08:29

1 Answers1

1

I have copied your code and compiled/run it and nothing is wrong on both my phone and emulator show as Figure 1 and 2.

It is worth to notify that the blue oval keeps static on emulator because the emulator does not have accelerometer. On the other hand, the blue oval keeps dynamic within a rectangle while I move or shake my phone. If the oval still keeps static on your phone, you should check your hardware and software. The hardware means the accelerometer chip on your phone while the software means your driver and HAL for the accelerometer.

It is easy to install some app like "android sensor box" to check if you can read 3-axis values from the accelerometer. And you can also look the log in logcat to check the sensor services.

My AndroidManifest.xml is as the following.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.so_problem"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.so_problem.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Figure 1 Run on emulator. enter image description here

Figure 2 Run on my phone. enter image description here

Tomas
  • 116
  • 6