Android proximity sensor when uncoverd - android

I work in project where i use proximity sensor my program should mute the phone when the proximity sensor is covered and restore it to normal when the sensor is uncovered. i managed to code for when the sensor is covered but i don't know the value for far when the sensor is uncovered i need a code to unmute when the sensor is uncovered.
#Override
public void onSensorChanged(SensorEvent event) {
audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}

public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if(event.sensor.getType()==Sensor.TYPE_PROXIMITY){
if(event.values[0]<5){ audio.setRingerMode(AudioManager.RINGER_MODE_youwant);}else{ audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);}
}
}

Here is the code for mute/unmute
AudioManager mAudioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
int current_volume =mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
//If you want to player is mute ,then set_volume variable is zero.Otherwise you may supply some value.
int set_volume=0;
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,set_volume, 0);

Related

Android switching between normal speaker and ear speaker

I'm trying to play an audio file through the default speaker or through the little speaker (used during calls).
Now i'm trying to choose between these 2 options using the proximity sensor.
Here's my code:
am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
am.setMode(AudioManager.STREAM_MUSIC);
m=MediaPlayer.create(getApplicationContext(), R.raw.a_meno_che_non);
m.setAudioStreamType(AudioManager.STREAM_MUSIC);
am.setSpeakerphoneOn(true);
SensorEventListener proximitySensorEventListener = new SensorEventListener(){
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
#Override
public void onSensorChanged(SensorEvent event) {
if(event.sensor.getType()==Sensor.TYPE_PROXIMITY){
if((int)(event.values[0])==myProximitySensor.getMaximumRange()){
am.setSpeakerphoneOn(true);
}
else {
am.setSpeakerphoneOn(false);
}
}
}
};
this works, but not very well because when switching between the speakers it loses a part of audio (about 2 seconds).
How can I solve this?

Detect event when hardware volume button is pressed in android

i am developing an app which listen volume events whenever hardware volume button is pressed. app can be in foreground or background. i have followed [this] (Is there a broadcast action for volume changes?) but its not working correctly. following issues i am facing
1) If I press the volume button once - the event is triggered 4-6 times.
2) If current volume is maximum and i increase the volume then event doesn't fire..
Please help me.
Try to use following code -
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)){
//Do something
}
return true;
}
for background - any way to detect volume key presses or volume changes with android service?
On a rooted device, you can use the Xposed framework to hook yourself into PhoneWindowManager. A good example is the Xposed Torch Module. Just decompile it and see how they do things.
You can detect this by polling AudioManager for current volume, it's not nice solution, but i don't know better.
private Handler handler;
public int volume;
private Runnable volumeUpdater = new Runnable() {
private int updatesInterval = 100;
#Override
public void run() {
if(volume != audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)){
//volume changed put logic here
}
handler.postDelayed(volumeUpdater, updatesInterval);
}
};

Android: Need app's Audio Volume Seekbar to change totally independed of the device volume

I need my in-app volume seekbar to change the volume of the audio in my app only. I need it to be totally separate from the main volume of the device. Everything I find changes the device volume when I change the slider in my app.
I want users to have the option of playing audio in my app at a low volume, but maybe still have their device turned up so phone calls and alarms and such could still be at their normal high volume.
How can I make a seekbar in my app only control audio in my app and not bother other device volumes? Here is the code I am using:
Called in onCreate method:
VolumeControls();
Outside of that:
private void VolumeControls() {
final TextView txtView = (TextView)findViewById(R.id.textView1);
SeekBar seekBar = (SeekBar)findViewById(R.id.sbVolume);
final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
seekBar.setMax(audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
seekBar.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC));
txtView.setText(String.valueOf(audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC)));
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar arg0){
}
public void onStartTrackingTouch(SeekBar arg0){
}
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
{
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
progress, 0);
txtView.setText(String.valueOf(progress));
}
});
}

How to test Proximity sensor on emulator ?

This is Code for getting proximity sensor.On sensor change i show a Toast message.
public class MainActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mSensor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
mSensorManager.registerListener(this, mSensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mSensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
#Override
public void onSensorChanged(SensorEvent event) {
Toast.makeText(this, "Sensor Changed", Toast.LENGTH_LONG).show();
if (event.values[0] == 0) {
Toast.makeText(this, "Screen off", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Screen on", Toast.LENGTH_LONG).show();
}
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}
For the testing this app i connect to the emulator via telnet like this telnet localhost 5554. The i change the proximity like this sensor set proximity 5. but it doesn't work. What is the wrong thing i did ?
This is achievable with following steps:
Open AS, run the emulator
Click on Extended controls
Choose Virtual sensors
In upper tab choose Additional sensors
And there you have it, play with proximity as you want
No, You cannot use proximity sensors on the emulator but you can test them on the real device because they contains that kind of sensors you want to test like accelerometer, gyroscope , magnetometer. But yes you can try gps like application in emulator using DDMS. You can use Genymotion with limited functionality on free version but real devices are advantageous.
One solution will be use a third party virtualized android machine like Genymotion .
The application has neat controls to change location data and sensor data , it even has built in java libraries to implement test cases

Change Media volume in Android?

Can I change the media volume? and how? I used this so far:
setVolumeControlStream(AudioManager.STREAM_MUSIC);
But have a seekbar and want to change the media volume, not ring volume.
So can someone show me how to just change the media volume at onCreate() and I fix the seekbar later.
The right method to use would be setStreamVolume on your AudioManager. It could looks like this
AudioManager audioManager =
(AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
[int value],
[if desired a flag]);
An example use of the flag is to get the beep when setting the volume so the user can hear the outcome. The flag for that would be AudioManager.FLAG_PLAY_SOUND.
You could use AudioManager.FLAG_SHOW_UI if you don't want to play a sound but display a toast with the current value. The use has to get a feedback tho. Doesn't matter if it is audible or visual.
To get the maximal valid value for the given stream you just call getStreamMaxVolume() on the AudioManager and get an integer back which represents ... well the maximal valid value for the volume.
private AudioManager audio;
Inside onCreate:
audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Override onKeyDown:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
return true;
default:
// return false;
// Update based on #Rene comment below:
return super.onKeyDown(keyCode, event);
}
}
You can use the following code to handle Volume using a SeekBar:
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
SeekBar sbVolumeBooster = (SeekBar) findViewById(R.id.sbVolumeBooster);
sbVolumeBooster.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
sbVolumeBooster.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
sbVolumeBooster.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
#Override
public void onStopTrackingTouch(SeekBar arg0)
{
}
#Override
public void onStartTrackingTouch(SeekBar arg0)
{
}
#Override
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
{
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
progress, 0); // 0 can also be changed to AudioManager.FLAG_PLAY_SOUND
}
});
Giving a 0 - in the flags avoids getting a visual and audio indicator .
That's good when you implement your own audio bar and indicator and you don't want android to add anything.
Use adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, flags);
http://developer.android.com/reference/android/media/AudioManager.html#adjustStreamVolume(int, int, int)

Categories

Resources