In the last
article, we discussed how to add the ability to switch tracks using the volume keys in the open source player
Vanilla Music , if the device is in your pocket (for example). In this article, we’ll continue to modify the main idea for which the next thought was - how to switch tracks without touching the smartphone, not unlocking it - in general with minimal effort.
As a result, it was decided to use the information from the proximity sensor
(Proximity sensor) . The essence of the idea is to switch the track when a signal about approaching from the sensor appears.
So, we proceed to the modification. To work with the proximity sensor, you need to enable its “listening” in the application. This was done in the last
article , but just in case I will give the configuration code:
private void setupSensor() { if (mSensorManager == null) mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); if (mShakeAction == Action.Nothing) { if (mSensorManager != null) mSensorManager.unregisterListener(this); } if(enable_defer_stop) mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_UI); mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY),SensorManager.SENSOR_DELAY_NORMAL); }
Now you need to track the change in the state of the proximity sensor. Like last time, let's turn to the
public void method
onSensorChanged (SensorEvent se) , whose role (judging by its name) is caused when the state of registered sensors changes. In the last article, we already determined which sensor gave the changes, but I also give here the tracking code:
')
@Override public void onSensorChanged(SensorEvent se) { if (se.sensor.getType() == Sensor.TYPE_PROXIMITY) { if( se.values[0] == 0) isproximity = true; else isproximity = false; } ....
The variable
isproximity - a member of the
PlayBackService class stores the last state received from the sensor (true or false). Now it is necessary to compare it with the previous state - to do this, add another
boolean variable
prev_isproximity , by default set
prev_isproximity = false and add the setting of this variable to the
OnSensorChanged function
@Override public void onSensorChanged(SensorEvent se) { if (se.sensor.getType() == Sensor.TYPE_PROXIMITY) { if( se.values[0] == 0) isproximity = true; else isproximity = false; prev_isproximity = isproximity; ....
We proceed to the main code of our functional - switching tracks according to the condition of changing the approximation. Switching should occur when the following expressions are true
(isproximity! = Prev_isproximity) and
(isproximity == true) . In order for the switch to work with a certain delay of the object above the proximity sensor (for example, 1 second), it is also necessary to add a definition of the difference between the current time and the previous time of information change from the sensor. To do this, we will declare in the class itself the variable
long mLastProximityChangeTime , which will store the time of the last change of information from the sensor. Based on the difference between the current time of changing information from the sensor and the time of the latter, we determine their difference, which we use to perform the actions:
@Override public void onSensorChanged(SensorEvent se) { if (se.sensor.getType() == Sensor.TYPE_PROXIMITY) { if( se.values[0] == 0) isproximity = true; else isproximity = false;
As a result, we got an
audio player that allows you to switch tracks just by holding your hand over the top of the smartphone (at the location of the proximity sensor), while spending quite a bit of time. In this again, the principle of Open Source shows itself from the best side! You can download the modified project source code in the
github repository.