aakash2310

Untitled

Oct 6th, 2022
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. package com.example.fourtysix;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.hardware.Sensor;
  6. import android.hardware.SensorEvent;
  7. import android.hardware.SensorEventListener;
  8. import android.hardware.SensorManager;
  9. import android.os.Bundle;
  10. import android.widget.TextView;
  11.  
  12. public class MainActivity extends AppCompatActivity implements SensorEventListener {
  13. TextView textView;
  14. SensorManager sm;
  15. Sensor motionSensor; //accelerator sensor
  16.  
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_main);
  21. textView = findViewById(R.id.textview1);
  22. sm = (SensorManager) getSystemService(SENSOR_SERVICE);
  23. motionSensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  24. }
  25. @Override
  26. public void onSensorChanged(SensorEvent sensorEvent){
  27. textView.setText("X:" + sensorEvent.values[0] + "\n"+
  28. "Y" +sensorEvent.values[1] + "\n" +
  29. "Z" + sensorEvent.values[2] + "\n");
  30. }
  31.  
  32. @Override
  33. public void onAccuracyChanged(Sensor sensor, int i) {
  34.  
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment