HarshBarash

Error

May 10th, 2021
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.63 KB | None | 0 0
  1. public class Lunokhod extends Activity implements LocationListener, SensorEventListener  {
  2.  
  3.         Button btn;
  4.  
  5.         private SensorManager sensorManager;
  6.         private Sensor lightSensor, mGravity;
  7.         private SensorEventListener lightEventListener;
  8.         private View light;
  9.         private float maxValue;
  10.         private TextView textView, speed, magnit;
  11.         public static DecimalFormat DECIMAL_FORMATTER;
  12.         private TextView xTextView, yTextView, zTextView;
  13.         private boolean isGravitySensorPresent;
  14.         private ImageView ImageView;
  15.         private float[] mmGravity = new float[3];
  16.         private float[] mGeomagnetic = new float[3];
  17.         private float azimuth = 0f;
  18.         private float currectAzimuth = 0f;
  19.  
  20.         private ImageView image;
  21.  
  22.         // record the compass picture angle turned
  23.         private float currentDegree = 0f;
  24.  
  25.         // device sensor manager
  26.         private SensorManager mSensorManager;
  27.  
  28.         TextView tvHeading;
  29.  
  30.  
  31.         @SuppressLint("ObsoleteSdkInt")
  32.         protected void onCreate(Bundle savedInstanceState) {
  33.                 super.onCreate(savedInstanceState);
  34.                 setContentView(R.layout.lunokhod2);
  35.                 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  36.  
  37.                 light = findViewById(R.id.light);
  38.                 sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
  39.                 lightSensor = sensorManager.getDefaultSensor(TYPE_LIGHT);
  40.                 textView = findViewById(R.id.lighttext);
  41.                 speed = findViewById(R.id.speed);
  42.                 magnit = findViewById(R.id.magnittext);
  43.                 // разпосзнать формат
  44.                 DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.ROOT);
  45.                 symbols.setDecimalSeparator('.');
  46.                 DECIMAL_FORMATTER = new DecimalFormat("#.000", symbols);
  47.                 xTextView = findViewById(R.id.gravityX);
  48.                 yTextView = findViewById(R.id.gravityY);
  49.                 zTextView = findViewById(R.id.gravityZ);
  50.                 ImageView = (ImageView)findViewById(R.id.compass);
  51.  
  52.                 image = (ImageView) findViewById(R.id.compass);
  53.  
  54.                 tvHeading  = (TextView)findViewById(R.id.tvHeading);
  55.  
  56.  
  57.  
  58.                 // initialize your android device sensor capabilities
  59.                 mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
  60.  
  61.                 sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
  62.  
  63.                 if(sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY)!= null)
  64.                 {
  65.                         mGravity = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
  66.                         isGravitySensorPresent = true;
  67.                 } else {
  68.                         xTextView.setText("Датчик остутсвует");
  69.                         isGravitySensorPresent = false;
  70.                 }
  71.  
  72.                 if(Build.VERSION.SDK_INT >= VERSION_CODES.M && checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
  73.                         !=PackageManager.PERMISSION_GRANTED) {
  74.                         requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1000);
  75.                 }
  76.                 else{
  77.                         doSpeed(); //если есть все разрешеня, выполняем программу
  78.                 }
  79.  
  80.                 this.updateSpeed(null);
  81.  
  82.  
  83.                 if (lightSensor == null) {
  84.                         Toast.makeText(this, "Датчик отсутсвует", Toast.LENGTH_SHORT).show();
  85.                 }
  86.  
  87.                 maxValue = lightSensor.getMaximumRange();
  88.  
  89.                 lightEventListener = new SensorEventListener() {
  90.                         @Override
  91.                         public void onSensorChanged(SensorEvent event) {
  92.                                 float value = event.values[0];
  93.                                 textView.setText( value + "лк");
  94.                                 int newValue = (int) (255 * value / maxValue);
  95.                                 light.setBackgroundColor(Color.rgb(newValue, newValue, newValue));
  96.  
  97.                         }
  98.  
  99.                         @Override
  100.                         public void onAccuracyChanged(Sensor sensor, int accuracy) {
  101.  
  102.                         }
  103.                 };
  104.  
  105.  
  106.  
  107.                 btn = (Button)findViewById( R.id.change);
  108.                 btn.setOnClickListener(new View.OnClickListener() {
  109.                         @Override
  110.                         public void onClick(View v) {
  111.                                 Intent i = new Intent(Lunokhod.this, MoonExpedition.class);
  112.                                 startActivity(i);
  113.                                 finish();
  114.                         }
  115.                 });
  116.  
  117.         }
  118.  
  119.         @Override
  120.         protected void onResume() {
  121.                 super.onResume();
  122.                 sensorManager.registerListener(lightEventListener, lightSensor, SensorManager.SENSOR_DELAY_FASTEST);
  123.                 sensorManager.registerListener((SensorEventListener) this,
  124.                         sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
  125.                         SensorManager.SENSOR_DELAY_NORMAL);
  126.  
  127.                 if(sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY)!=null);
  128.                 sensorManager.registerListener(this, mGravity, SensorManager.SENSOR_DELAY_FASTEST);
  129.  
  130.                 mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
  131.                         SensorManager.SENSOR_DELAY_GAME);
  132.         }
  133.  
  134.         @SuppressLint("SetTextI18n")
  135.         @Override
  136.         public void onSensorChanged(SensorEvent event) {
  137.                 if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
  138.                         //даем значение xyz
  139.                         float magX = event.values[0];
  140.                         float magY = event.values[1];
  141.                         float magZ = event.values[2];
  142.  
  143.                         double magnitude = Math.sqrt((magX * magX) + (magY * magY) + (magZ * magZ));
  144. //            float magnitudeF = (float) magnitude;
  145.  
  146.                         magnit.setText(DECIMAL_FORMATTER.format(magnitude) + " \u00B5Тесла");
  147.                 }
  148.                 xTextView.setText(event.values[0]+ "М/c²");
  149.                 yTextView.setText(event.values[1]+ "М/c²");
  150.                 zTextView.setText(event.values[2]+ "М/c²");
  151.  
  152.                 float degree = Math.round(event.values[0]);
  153.  
  154.                 tvHeading.setText((int) degree);
  155.  
  156.                 RotateAnimation ra = new RotateAnimation(
  157.                         currentDegree,
  158.                         -degree,
  159.                         Animation.RELATIVE_TO_SELF, 0.5f,
  160.                         Animation.RELATIVE_TO_SELF,
  161.                         0.5f);
  162.  
  163.                 ra.setDuration(210);
  164.  
  165.                 ra.setFillAfter(true);
  166.  
  167.                 image.startAnimation(ra);
  168.                 currentDegree = -degree;
  169.                         }
  170.  
  171.  
  172.         @Override
  173.         public void onAccuracyChanged(Sensor sensor, int accuracy) {
  174.  
  175.         }
  176.  
  177.         @Override
  178.         protected void onPause() {
  179.                 super.onPause();
  180.                 sensorManager.unregisterListener(lightEventListener);
  181.                 sensorManager.unregisterListener(this);
  182.                 if(sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY)!=null);
  183.                 sensorManager.unregisterListener(this, mGravity);
  184.                 sensorManager.unregisterListener(this);
  185.  
  186.                 mSensorManager.unregisterListener(this);
  187.         }
  188.  
Advertisement
Add Comment
Please, Sign In to add comment