Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Lunokhod extends Activity implements LocationListener, SensorEventListener {
- Button btn;
- private SensorManager sensorManager;
- private Sensor lightSensor, mGravity;
- private SensorEventListener lightEventListener;
- private View light;
- private float maxValue;
- private TextView textView, speed, magnit;
- public static DecimalFormat DECIMAL_FORMATTER;
- private TextView xTextView, yTextView, zTextView;
- private boolean isGravitySensorPresent;
- private ImageView ImageView;
- private float[] mmGravity = new float[3];
- private float[] mGeomagnetic = new float[3];
- private float azimuth = 0f;
- private float currectAzimuth = 0f;
- private ImageView image;
- // record the compass picture angle turned
- private float currentDegree = 0f;
- // device sensor manager
- private SensorManager mSensorManager;
- TextView tvHeading;
- @SuppressLint("ObsoleteSdkInt")
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.lunokhod2);
- getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
- light = findViewById(R.id.light);
- sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
- lightSensor = sensorManager.getDefaultSensor(TYPE_LIGHT);
- textView = findViewById(R.id.lighttext);
- speed = findViewById(R.id.speed);
- magnit = findViewById(R.id.magnittext);
- // разпосзнать формат
- DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.ROOT);
- symbols.setDecimalSeparator('.');
- DECIMAL_FORMATTER = new DecimalFormat("#.000", symbols);
- xTextView = findViewById(R.id.gravityX);
- yTextView = findViewById(R.id.gravityY);
- zTextView = findViewById(R.id.gravityZ);
- ImageView = (ImageView)findViewById(R.id.compass);
- image = (ImageView) findViewById(R.id.compass);
- tvHeading = (TextView)findViewById(R.id.tvHeading);
- // initialize your android device sensor capabilities
- mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
- sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
- if(sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY)!= null)
- {
- mGravity = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
- isGravitySensorPresent = true;
- } else {
- xTextView.setText("Датчик остутсвует");
- isGravitySensorPresent = false;
- }
- if(Build.VERSION.SDK_INT >= VERSION_CODES.M && checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
- !=PackageManager.PERMISSION_GRANTED) {
- requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1000);
- }
- else{
- doSpeed(); //если есть все разрешеня, выполняем программу
- }
- this.updateSpeed(null);
- if (lightSensor == null) {
- Toast.makeText(this, "Датчик отсутсвует", Toast.LENGTH_SHORT).show();
- }
- maxValue = lightSensor.getMaximumRange();
- lightEventListener = new SensorEventListener() {
- @Override
- public void onSensorChanged(SensorEvent event) {
- float value = event.values[0];
- textView.setText( value + "лк");
- int newValue = (int) (255 * value / maxValue);
- light.setBackgroundColor(Color.rgb(newValue, newValue, newValue));
- }
- @Override
- public void onAccuracyChanged(Sensor sensor, int accuracy) {
- }
- };
- btn = (Button)findViewById( R.id.change);
- btn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent i = new Intent(Lunokhod.this, MoonExpedition.class);
- startActivity(i);
- finish();
- }
- });
- }
- @Override
- protected void onResume() {
- super.onResume();
- sensorManager.registerListener(lightEventListener, lightSensor, SensorManager.SENSOR_DELAY_FASTEST);
- sensorManager.registerListener((SensorEventListener) this,
- sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
- SensorManager.SENSOR_DELAY_NORMAL);
- if(sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY)!=null);
- sensorManager.registerListener(this, mGravity, SensorManager.SENSOR_DELAY_FASTEST);
- mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
- SensorManager.SENSOR_DELAY_GAME);
- }
- @SuppressLint("SetTextI18n")
- @Override
- public void onSensorChanged(SensorEvent event) {
- if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
- //даем значение xyz
- float magX = event.values[0];
- float magY = event.values[1];
- float magZ = event.values[2];
- double magnitude = Math.sqrt((magX * magX) + (magY * magY) + (magZ * magZ));
- // float magnitudeF = (float) magnitude;
- magnit.setText(DECIMAL_FORMATTER.format(magnitude) + " \u00B5Тесла");
- }
- xTextView.setText(event.values[0]+ "М/c²");
- yTextView.setText(event.values[1]+ "М/c²");
- zTextView.setText(event.values[2]+ "М/c²");
- float degree = Math.round(event.values[0]);
- tvHeading.setText((int) degree);
- RotateAnimation ra = new RotateAnimation(
- currentDegree,
- -degree,
- Animation.RELATIVE_TO_SELF, 0.5f,
- Animation.RELATIVE_TO_SELF,
- 0.5f);
- ra.setDuration(210);
- ra.setFillAfter(true);
- image.startAnimation(ra);
- currentDegree = -degree;
- }
- @Override
- public void onAccuracyChanged(Sensor sensor, int accuracy) {
- }
- @Override
- protected void onPause() {
- super.onPause();
- sensorManager.unregisterListener(lightEventListener);
- sensorManager.unregisterListener(this);
- if(sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY)!=null);
- sensorManager.unregisterListener(this, mGravity);
- sensorManager.unregisterListener(this);
- mSensorManager.unregisterListener(this);
- }
Advertisement
Add Comment
Please, Sign In to add comment