Thursday, 12 September 2013

Accel Sensor update on button push

Accel Sensor update on button push

I made this program for Sensor output with two buttons, One button start
registerListener, Off SensorEventListener and unregisterListener. Another
button (Update) i want it to display (output) the sensor position at the
moment i pushed it? thank you for help. Main.xml is:
package jp.dsc.sensoraccel;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends Activity implements SensorEventListener {
private SensorManager sensorManager;
TextView xCoor; // declare X axis object
TextView yCoor; // declare Y axis object
TextView zCoor; // declare Z axis object
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
xCoor = (TextView) findViewById(R.id.xcoor); // create X axis object
yCoor = (TextView) findViewById(R.id.ycoor); // create Y axis object
zCoor = (TextView) findViewById(R.id.zcoor); // create Z axis object
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event) {
// check sensor type
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
// assign directions
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
xCoor.setText("Accel X: " + x);
yCoor.setText("Accel Y: " + y);
zCoor.setText("Accel Z: " + z);
}
}
public void onButtonClicked(View v) {
//Tost declaration
Context context = getApplicationContext();
CharSequence msg = "Sensor Updated!";
int duration = Toast.LENGTH_SHORT;
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_FASTEST);
final SensorEventListener listener = this;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
sensorManager.unregisterListener(listener);
}
}, 200);
Toast toast = Toast.makeText(context, msg, duration);
toast.show();
}
public void onToggleClicked(View view) {
// Check if the toggleButton is on?
boolean on = ((ToggleButton) view).isChecked();
//Tost declaration
Context context = getApplicationContext();
CharSequence msg1 = "Continue!", msg2 = "Update Stopped!";
int duration = Toast.LENGTH_SHORT;
if (on) {
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_FASTEST);
Toast toast = Toast.makeText(context, msg1, duration);
toast.show();
Button btn=(Button)findViewById(R.id.button1);
btn.setEnabled(false);
}
else {
final SensorEventListener listener = this;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
sensorManager.unregisterListener(listener);
}
}, 200);
Toast toast = Toast.makeText(context, msg2, duration);
toast.show();
Button btn=(Button)findViewById(R.id.button1);
btn.setEnabled(true);
}
}
}

No comments:

Post a Comment