F
F
Flabber2015-12-16 11:49:47
Android
Flabber, 2015-12-16 11:49:47

How to use distance sensor in android? And how to use additional content?

Good afternoon!
My application (game) should use a distance sensor.

public class SensorActivity extends Activity implements SensorEventListener {
  private SensorManager mSensorManager;
  private Sensor mProximity;

  @Override
  public final void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Get an instance of the sensor service, and use that to get an instance of
    // a particular sensor.
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mProximity = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
  }

  @Override
  public final void onAccuracyChanged(Sensor sensor, int accuracy) {
    // Do something here if sensor accuracy changes.
  }

  @Override
  public final void onSensorChanged(SensorEvent event) {
    float distance = event.values[0];
    // Do something with this sensor data.
  }

  @Override
  protected void onResume() {
    // Register a listener for the sensor.
    super.onResume();
    mSensorManager.registerListener(this, mProximity, SensorManager.SENSOR_DELAY_NORMAL);
  }

  @Override
  protected void onPause() {
    // Be sure to unregister the sensor when the activity pauses.
    super.onPause();
    mSensorManager.unregisterListener(this);
  }
}

The environment writes that the onSensorChanged, onAccuracyChanged methods must be implemented, but I still don’t understand, I create a separate SensorActivity class, SetContentView points to the activity in which the sensor should work, there is a TextView, I create an instance of this TextView in onSensorChanged and display the text let "I sensor". Obviously, this is not done so))) But it is impossible to figure it out correctly.
The second question is how to use the content in the application. Let's say the application should randomly play music. file from pre-selected songs. I don't know how to create a semblance of a content base and how to use it.
I am a beginner, but I caught inspiration, I have already made the interface in principle and are separated from the finished application, only these two implementations.
I will be very glad for any hint. Already a headache from 2 days without a resultant search.
UPD: I figured out the first question)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GavriKos, 2015-12-16
@GavriKos

I create an instance of this TextView in onSensorChanged and display the text, let it be "I am a sensor". Obviously this is not how it works.)

I don't see this code for you.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question