문제

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); 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top