The manifest file
<uses-permission android:name="android.permission.LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
The layout file(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/accText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
The Activity Class(AccelerometerTest.java)
package com.sarath.accelerometertest;
import android.app.Activity;
import android.os.Bundle;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.widget.TextView;
public class AccelerometerTest extends Activity {
private TextView accText;
private SensorManager myManager;
private List
private Sensor accSensor;
private float oldX, oldY, oldZ = 0f;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
accText = (TextView)findViewById(R.id.accText);
// Set Sensor + Manager
myManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
sensors = myManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
if(sensors.size() > 0)
{
accSensor = sensors.get(0);
}
}
private void updateTV(float x, float y, float z)
{
/*float thisX = x - oldX * 10;
float thisY = y - oldY * 10;
float thisZ = z - oldZ * 10;
*/
float thisX = (x - oldX) * 10;
float thisY = (y - oldY) * 10;
float thisZ = (z - oldZ) * 10;
accText.setText("x: " + Math.round(thisX) + ";\n y:" + Math.round(thisY) + ";\n z: " + Math.round(thisZ));
oldX = x;
oldY = y;
oldZ = z;
}
private final SensorEventListener mySensorListener = new SensorEventListener()
{
public void onSensorChanged(SensorEvent event)
{
updateTV(event.values[0],
event.values[1],
event.values[2]);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
};
@Override
protected void onResume()
{
super.onResume();
myManager.registerListener(mySensorListener, accSensor, SensorManager.SENSOR_DELAY_GAME);
}
@Override
protected void onStop()
{
myManager.unregisterListener(mySensorListener);
super.onStop();
}
}
No comments:
Post a Comment