import androidx.appcompat.app.AppCompatActivity;
import android.hardware.Sensor;
import android.hardware.SensorDirectChannel;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
textView1 = (TextView) findViewById(R.id.Values);
list = sm.getSensorList(Sensor.TYPE_ACCELEROMETER);
if (list.size() > 0) {
sm.registerListener(sel, (Sensor) list.get(0), SensorDirectChannel.RATE_VERY_FAST);
} else {
Toast.makeText(getBaseContext(), "Error: No Accelerometer.", Toast.LENGTH_LONG).show();
}
}
SensorManager sm = null;
TextView textView1 = null;
List list;
StringBuilder info = new StringBuilder();
final SensorEventListener sel = new SensorEventListener() {
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
String result0 = String.format("x: " + "%.20f", values[0]);
String result1 = String.format("y: " + "%.20f", values[1]);
String result2 = String.format("z: " + "%.20f", values[2]);
info.append(result0 + "\n" + result1 + "\n" + result2 + "\n");
//textView1.setText(result);
textView1.setText("x: " + values[0] + "\ny: " + values[1] + "\nz: " + values[2] + "\n");
}
};
@Override
protected void onStop() {
if (list.size() > 0) {
sm.unregisterListener(sel);
}
super.onStop();
}
}
import androidx.appcompat.app.AppCompatActivity;
import android.hardware.Sensor;
import android.hardware.SensorDirectChannel;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
textView1 = (TextView) findViewById(R.id.Values);
list = sm.getSensorList(Sensor.TYPE_ACCELEROMETER);
if (list.size() > 0) {
sm.registerListener(sel, (Sensor) list.get(0), SensorManager.SENSOR_DELAY_FASTEST);
} else {
Toast.makeText(getBaseContext(), "Error: No Accelerometer.", Toast.LENGTH_LONG).show();
}
}
SensorManager sm = null;
TextView textView1 = null;
List list;
StringBuilder info = new StringBuilder();
final SensorEventListener sel = new SensorEventListener() {
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
String result0 = String.format("x: " + "%.20f", values[0]);
String result1 = String.format("y: " + "%.20f", values[1]);
String result2 = String.format("z: " + "%.20f", values[2]);
info.append(result0 + "\n" + result1 + "\n" + result2 + "\n");
//textView1.setText(result);
textView1.setText("x: " + values[0] + "\ny: " + values[1] + "\nz: " + values[2] + "\n");
}
};
@Override
protected void onStop() {
if (list.size() > 0) {
sm.unregisterListener(sel);
}
super.onStop();
}
}