2013-04-05 252 views
0

我在使用接近傳感器的項目中工作,我的程序應當在接近傳感器被覆蓋時使手機靜音,並在傳感器被覆蓋時將其恢復正常。我設法在傳感器被覆蓋時進行編碼,但當傳感器未被覆蓋時,我不知道遠處的值,當傳感器被發現時,我需要一個代碼來取消靜音。Android接近傳感器發現時

@Override 
public void onSensorChanged(SensorEvent event) { 
    audio.setRingerMode(AudioManager.RINGER_MODE_SILENT); 

} 
+0

外觀到單證代碼 http://developer.android.com/reference/android/hardware/SensorEvent.html 和 http://developer.android.com/reference/android/hardware/SensorManager.html – 2013-04-05 05:48:16

+0

檢查這個我會得到釐米的距離,我希望不管怎麼樣嘗試http://www.java2s.com/Code/Android/Hardware/PROXIMITYSensor熱媒 – 2013-04-05 05:55:16

回答

0
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);} 
    } 
    } 
0

這裏是靜音/取消靜音

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);